Home

Awesome

async-control npmjs.com The MIT License

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.

code climate standard code style travis build status coverage status dependency status

Install

npm i async-control --save

Usage

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 AsyncControl with options to control enabling/disabling options.settle, passing custom iterator and pass different hooks - before, after, etc.

Params

.series

Iterate over value in series flow. The async.mapSeries method is used.

Params

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 value in parallel flow. The async.map method is used.

Params

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 series or parallel method. Can be used to create settleSeries or settleParallel methods for example.

Params

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.define method. This also can be done by passing function to options.iterator. It recieves app and options and must return function which is directly passed to async.map and/or async.mapSeries methods.

Params

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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github