-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"node": true, | ||
"strict": true, | ||
"eqnull": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var series = require('./series'); | ||
var parallel = require('./parallel'); | ||
var settleSeries = require('./settleSeries'); | ||
var settleParallel = require('./settleParallel'); | ||
|
||
module.exports = { | ||
series: series, | ||
parallel: parallel, | ||
settleSeries: settleSeries, | ||
settleParallel: settleParallel, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
|
||
function buildOnComplete(done){ | ||
function onSettled(error, result){ | ||
var settledErrors = _.where(result, { state: 'error' }); | ||
var settledResults = _.where(result, { state: 'success' }); | ||
|
||
var errors; | ||
if(settledErrors.length){ | ||
errors = _.pluck(settledErrors, 'value'); | ||
} | ||
|
||
var results; | ||
if(settledResults.length){ | ||
results = _.pluck(settledResults, 'value'); | ||
} | ||
|
||
error = error || errors; | ||
if(error){ | ||
return done(error, results); | ||
} | ||
|
||
done(undefined, results); | ||
} | ||
|
||
return onSettled; | ||
} | ||
|
||
module.exports = buildOnComplete; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "bach", | ||
"version": "0.0.0", | ||
"description": "Compose async functions", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "tap ./test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/phated/bach" | ||
}, | ||
"keywords": [ | ||
"async", | ||
"compose", | ||
"fluent", | ||
"composing" | ||
], | ||
"author": "Blaine Bublitz <blaine@iceddev.com> (http://iceddev.com/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/phated/bach/issues" | ||
}, | ||
"homepage": "https://github.com/phated/bach", | ||
"devDependencies": { | ||
"tap": "~0.4.8" | ||
}, | ||
"dependencies": { | ||
"async-settle": "~0.1.0", | ||
"async-done": "~0.2.0", | ||
"async": "~0.2.10", | ||
"lodash": "~2.4.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var async = require('async'); | ||
var asyncDone = require('async-done'); | ||
|
||
function buildParallel(){ | ||
var args = _.flatten(arguments); | ||
|
||
function parallel(done){ | ||
async.map(args, asyncDone, done); | ||
} | ||
|
||
return parallel; | ||
} | ||
|
||
module.exports = buildParallel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var async = require('async'); | ||
var asyncDone = require('async-done'); | ||
|
||
function buildSeries(){ | ||
var args = _.flatten(arguments); | ||
|
||
function series(done){ | ||
async.mapSeries(args, asyncDone, done); | ||
} | ||
|
||
return series; | ||
} | ||
|
||
module.exports = buildSeries; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var async = require('async'); | ||
var asyncSettle = require('async-settle'); | ||
|
||
var onSettled = require('./lib/onSettled'); | ||
|
||
function buildParallel(){ | ||
var args = _.flatten(arguments); | ||
|
||
function settleParallel(done){ | ||
async.map(args, asyncSettle, onSettled(done)); | ||
} | ||
|
||
return settleParallel; | ||
} | ||
|
||
module.exports = buildParallel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var async = require('async'); | ||
var asyncSettle = require('async-settle'); | ||
|
||
var onSettled = require('./lib/onSettled'); | ||
|
||
function buildParallel(){ | ||
var args = _.flatten(arguments); | ||
|
||
function settleSeries(done){ | ||
async.mapSeries(args, asyncSettle, onSettled(done)); | ||
} | ||
|
||
return settleSeries; | ||
} | ||
|
||
module.exports = buildParallel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
|
||
var bach = require('../'); | ||
|
||
function fn1(done){ | ||
done(null, 1); | ||
} | ||
|
||
function fn2(done){ | ||
setTimeout(function(){ | ||
done(null, 2); | ||
}, 500); | ||
} | ||
|
||
function fn3(done){ | ||
done(null, 3); | ||
} | ||
|
||
function fnError(done){ | ||
done(new Error('An Error Occurred')); | ||
} | ||
|
||
test('should execute functions in parallel and call callback with results on completion', function(t){ | ||
bach.parallel(fn1, fn2, fn3)(function(error, results){ | ||
t.notOk(error, 'error should be undefined'); | ||
t.ok(results, 'results should be defined'); | ||
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('should execute functions in parallel and call callback with error on an error', function(t){ | ||
function slowFn(done){ | ||
setTimeout(function(){ | ||
t.fail('slow function should not be called'); | ||
done(null, 2); | ||
}, 500); | ||
} | ||
bach.parallel(fn1, slowFn, fn3, fnError)(function(error, results){ | ||
t.ok(error, 'error should be defined'); | ||
t.ok(results, 'results should be defined'); | ||
t.deepEqual(results, [1, , 3, undefined], 'results should be [1, , 3, undefined]'); | ||
t.ok(error instanceof Error, 'error should be instance of an Error'); | ||
t.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
|
||
var bach = require('../'); | ||
|
||
function fn1(done){ | ||
done(null, 1); | ||
} | ||
|
||
function fn2(done){ | ||
setTimeout(function(){ | ||
done(null, 2); | ||
}, 500); | ||
} | ||
|
||
function fn3(done){ | ||
done(null, 3); | ||
} | ||
|
||
function fnError(done){ | ||
done(new Error('An Error Occurred')); | ||
} | ||
|
||
test('should execute functions in series and call callback with results on completion', function(t){ | ||
bach.series(fn1, fn2, fn3)(function(error, results){ | ||
t.notOk(error, 'error should be undefined'); | ||
t.ok(results, 'results should be defined'); | ||
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('should execute functions in series and call callback with error on an error', function(t){ | ||
function slowFn(done){ | ||
setTimeout(function(){ | ||
t.ok(true, 'slow function should be called'); | ||
done(null, 2); | ||
}, 500); | ||
} | ||
bach.series(fn1, slowFn, fn3, fnError)(function(error, results){ | ||
t.ok(error, 'error should be defined'); | ||
t.ok(results, 'results should be defined'); | ||
t.deepEqual(results, [1, 2, 3, undefined], 'results should be [1, 2, 3, undefined]'); | ||
t.ok(error instanceof Error, 'error should be instance of an Error'); | ||
t.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
|
||
var bach = require('../'); | ||
|
||
function fn1(done){ | ||
done(null, 1); | ||
} | ||
|
||
function fn2(done){ | ||
setTimeout(function(){ | ||
done(null, 2); | ||
}, 500); | ||
} | ||
|
||
function fn3(done){ | ||
done(null, 3); | ||
} | ||
|
||
function fnError(done){ | ||
done(new Error('An Error Occurred')); | ||
} | ||
|
||
test('should execute functions in parallel and call callback with results on settled', function(t){ | ||
bach.settleParallel(fn1, fn2, fn3)(function(errors, results){ | ||
t.notOk(errors, 'errors should be undefined'); | ||
t.ok(results, 'results should be defined'); | ||
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('should execute functions in parallel and call callback with errors on settled', function(t){ | ||
function slowFn(done){ | ||
setTimeout(function(){ | ||
t.ok(true, 'slow function should be called'); | ||
done(null, 2); | ||
}, 500); | ||
} | ||
bach.settleParallel(fn1, slowFn, fn3, fnError)(function(errors, results){ | ||
t.ok(errors, 'errors should be defined'); | ||
t.ok(results, 'results should be defined'); | ||
t.ok(Array.isArray(errors), 'errors should be an array'); | ||
t.ok(errors[0] instanceof Error, 'errors should be an array of Error instances'); | ||
t.ok(Array.isArray(results), 'results should be an array'); | ||
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]') | ||
t.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
var test = require('tap').test; | ||
|
||
var bach = require('../'); | ||
|
||
function fn1(done){ | ||
done(null, 1); | ||
} | ||
|
||
function fn2(done){ | ||
setTimeout(function(){ | ||
done(null, 2); | ||
}, 500); | ||
} | ||
|
||
function fn3(done){ | ||
done(null, 3); | ||
} | ||
|
||
function fnError(done){ | ||
done(new Error('An Error Occurred')); | ||
} | ||
|
||
test('should execute functions in series and call callback with results on settled', function(t){ | ||
bach.settleSeries(fn1, fn2, fn3)(function(errors, results){ | ||
t.notOk(errors, 'errors should be undefined'); | ||
t.ok(results, 'results should be defined'); | ||
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('should execute functions in series and call callback with errors on settled', function(t){ | ||
function slowFn(done){ | ||
setTimeout(function(){ | ||
t.ok(true, 'slow function should be called'); | ||
done(null, 2); | ||
}, 500); | ||
} | ||
bach.settleSeries(fn1, slowFn, fn3, fnError)(function(errors, results){ | ||
t.ok(errors, 'errors should be defined'); | ||
t.ok(results, 'results should be defined'); | ||
t.ok(Array.isArray(errors), 'errors should be an array'); | ||
t.ok(Array.isArray(results), 'results should be an array'); | ||
t.ok(errors[0] instanceof Error, 'errors should be an array of Error instances'); | ||
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]'); | ||
t.end(); | ||
}); | ||
}); |