Compose your control flow with absolute elegance. Support async/await, callbacks, thunks, generators, promises, observables, child processes and streams. Can power applications that need to have plugins. Useful for creating task, test and bench runners. Built with backward compatibility and future stability in mind.
npm i benz --save
npm test
For more use-cases see the tests
var benz = require('benz')
Create a new instance of
Benz
.
[options]
{Object} Initialize with default options.
Example
var Benz = require('benz')
var benz = new Benz()
Used internally to create
.parallel
and.series
methods.
<method>
{String} all available now-and-later methods orseries
, orparallel
returns
{Function} composed function
Example
var fs = require('fs')
var benz = require('benz')
var series = benz().compose('series')
var done = series([
function (fp, encoding, next) {
fs.readFile(fp, encoding, next)
},
function (content, next) {
var name = JSON.parse(content).name
next(null, name)
}
])
done('./package.json', 'utf8', function (err, res) {
console.log(err) //=> null
console.log(res)
//=> ['{\n "name": "benz",\n "version": "0.4.0" ...', 'benz']
})
Run
fns
(plugins stack) in series.
<fns>
{Function|Array|Object} plugins stack[extensions]
{Object} passed to now-and-laterreturns
{Function} final done callback
Example
var done = benz.series([
function one (initial, next) {
setTimeout(function () {
console.log('second')
next(null, initial + 555)
}, Math.random() * 50)
},
function two (initial, next) {
setTimeout(function () {
console.log('third')
next(null, initial + 333)
}, Math.random() * 200)
},
function three (initial, next) {
setTimeout(function () {
console.log('first')
next(null, initial + 111)
}, 0)
}
])
done(222, function (err, res) {
//=> 'second'
//=> 'third'
//=> 'first'
console.log(err, res)
//=> [777, 555, 111]
})
Run
fns
(plugins stack) in paralell and maintain order of the results.
<fns>
{Function|Array|Object} plugins stack[extensions]
{Object} passed to now-and-laterreturns
{Function} final done callback
Example
var done = benz.parallel([
function one (initial, next) {
setTimeout(function () {
console.log('second')
next(null, initial + 300)
}, Math.random() * 50)
},
function two (initial, next) {
setTimeout(function () {
console.log('third')
next(null, initial + 100)
}, Math.random() * 200)
},
function three (initial, next) {
setTimeout(function () {
console.log('first')
next(null, initial + 444)
}, 0)
}
])
done(100, function (err, res) {
//=> 'first'
//=> 'second'
//=> 'third'
console.log(err, res)
//=> [400, 200, 544]
})
Alias of
.series
and.parallel
. By default will run the stack in series, otherwise in parallel, but only if parallel option is enabled.
<fns>
{Function|Array|Object} plugins stack[extensions]
{Object} passed to now-and-laterreturns
{Function} final done callback
Example
var fs = require('fs')
var done = benz.enable('onlylast').run([
fs.readFile,
function (content, next) {
next(null, JSON.parse(content).name)
}
])
done('./package.json', 'utf8', function (err, res) {
console.log(err, res) //=> null 'benz'
})
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.