async-control v0.5.0
async-control

Ultimate asynchronous control flow goodness with built-in hook system and compose, series, define and parallel methods. Uses async.map and async.mapSeries methods. Allows passing custom iterator function.
Install
npm i async-control --saveUsage
For more use-cases see the tests
const fs = require('fs')
const asyncControl = require('async-control')
asyncControl.series([
function one (next) {
console.log('first')
fs.readFile('package.json', 'utf8', next)
},
function two (next) {
setTimeout(function () {
console.log('second')
fs.stat('not exist', next)
}, 100)
},
function three (next) {
console.log('third')
next(null, 123)
}
], {settle: false}, function done (err, res) {
// => first
// => second
// => third
console.log('err:', err)
// => if `options.settle` is true - `undefined`, otherwise thrown Error
console.log('res:', res) // => array of results
// => res[0] is content of package.json file
// => res[1] is `undefined`, because second function throws Error (ENOENT)
// if `options.settle` is true
// - res[1] will be Error
// - res[2] will be 123
})AsyncControl
Initialize
AsyncControlwithoptionsto control enabling/disablingoptions.settle, passing customiteratorand pass different hooks - before, after, etc.
Params
options{Object=}
.series
Iterate over
valuein series flow. Theasync.mapSeriesmethod is used.
Params
value{Object|Array|Function}: The value to iterate over.options{Object=}: Can pass different hooks - before, after, beforeEach, afterEach.done{Function=}: If not passed, thunk is returned (function that accepts callback).returns{Function}: Orundefinedifdoneis passed.
Example
var fs = require('fs')
var asyncControl = require('async-control')
asyncControl.series([
function one (cb) {
cb(null, 'foo')
},
function two (cb) {
fs.readFile('not exist', cb)
},
function three (cb) {
cb(null, 'bar')
}
], console.log) //=> ENOENT Error, ['foo', undefined].parallel
Iterate over
valuein parallel flow. Theasync.mapmethod is used.
Params
value{Object|Array|Function}: The value to iterate over.options{Object=}: Can pass different hooks - before, after, beforeEach, afterEach.done{Function=}: If not passed, thunk is returned (function that accepts callback).returns{Function}: Orundefinedifdoneis passed.
Example
var fs = require('fs')
var asyncControl = require('async-control')
asyncControl.parallel([
function one (next) {
setTimeout(function () {
console.log('first')
next(null, 100)
}, Math.random() * 50)
},
function two (next) {
setTimeout(function () {
console.log('second')
next(null, 700)
}, Math.random() * 200)
},
function three (next) {
setTimeout(function () {
console.log('third')
next(null, 2000)
}, 0)
}
], function done (err, res) {
// => third
// => first
// => second
console.log(err) // => null
console.log(res) // => [2000, 100, 700]
}).compose
Compose
seriesorparallelmethod. Can be used to createsettleSeriesorsettleParallelmethods for example.
Params
flow{String}: Type of flow, one of'series'or'parallel'.options{Object=}: Can pass different hooks - before, after, beforeEach, afterEach.returns{Function}: Composedseriesorparallelmethod, depends onflow.
Example
var fs = require('fs')
var asyncControl = require('async-control')
// the internal `.series` method is created this way - using `.compose`
var series = asyncControl.compose('series')
series([
function one (cb) {
cb(null, 123)
},
function two (cb) {
fs.readFile('not exist', cb)
},
function three (cb) {
cb(null, 456)
}
], {settle: true}, console.log) //=> null, [123, ENOENT Error, 456]Custom iterator
Below example shows how to create your own iterator using
asyncControl.definemethod. This also can be done by passing function tooptions.iterator. It recievesappandoptionsand must return function which is directly passed toasync.mapand/orasync.mapSeriesmethods.
Params
app{Object}: instance of AsyncControlopts{Object}: theappoptionsreturns{Function}: iterator passed directly to async.map / async.mapSeries
Example
const util = require('util')
const asyncControl = require('async-control')
asyncControl.define('iterator', function customIterator (app, options) {
// this === app
return iterator (fn, next) {
// this === app
console.log(util.inspect(fn))
next()
}
})
asyncControl.series([
function first (next) { next(null, 1) },
function second (next) { next(null, 2) },
function third (next) { next(null, 3) }
], function (err, res) {
// => [Function: first]
// => [Function: second]
// => [Function: third]
console.log(err, res)
// => null, [1, 2, 3]
})Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.