Skip to content

Ultimate asynchronous control flow goodness with built-in hook system on top of [async][]. Uses `async.map` and` async.mapSeries` methods.

License

Notifications You must be signed in to change notification settings

tunnckoCore/async-control

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
})

Initialize AsyncControl with options to control enabling/disabling options.settle, passing custom iterator and pass different hooks - before, after, etc.

Params

  • options {Object=}

Iterate over value in series flow. The async.mapSeries method 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}: Or undefined if done is 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]

Iterate over value in parallel flow. The async.map method 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}: Or undefined if done is 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 series or parallel method. Can be used to create settleSeries or settleParallel methods 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}: Composed series or parallel method, depends on flow.

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

  • app {Object}: instance of AsyncControl
  • opts {Object}: the app options
  • returns {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.

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

About

Ultimate asynchronous control flow goodness with built-in hook system on top of [async][]. Uses `async.map` and` async.mapSeries` methods.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published