waterfall-exec
Executes an array of Promise
one after another.
Can also listen to the array
for new items.
Install
$ yarn add waterfall-exec
Usage
const
waterfallExec = require('waterfall-exec'),
{Waterfall} = waterfallExec;
let
jobs = [
() => Promise.resolve(),
() => Promise.resolve('some value'),
someValue => Promise.resolve(someValue)
],
waterfall = new Waterfall();
waterfall.exec(jobs)
.then(result => {
console.log(result); // logs 'some value' into the console
});
Waterfall
uses standard Promise
chain to synchronous execution of an array of asynchronous functions. Just pass an array of functions and Waterfall
starts to execute them one by one.
API
Waterfall.exec(jobs [, options])
jobs | Array
Array
of function
items. Each function
should return either Promise
or some data to be passed as resolved data.
options | object
optional
-
waitForItems |
boolean
optional causes main Promise is never resolved and Waterfall executes every new job added to thejobs
array. Default isfalse
. -
waitTimeout |
number
optional timeout forwaitForItems
. Ifundefined
, there is no timeout andwaterfall
will tick forever. Default isundefined
. -
checkInterval |
number
optional interval in miliseconds forwaitForItems
. Default is1000
. -
onNewPromise |
function
optional function to be called when new job starts. Accepts the stepPromise
as the first argument.
Waterfall.stop()
Stops the execution and clears the jobs
array. Also sets waitForItems
to false
, so if you want to resume the execution, you need to call Waterfall.exec()
again.
Testing
Tests are using AVA library
$ yarn test // run all tests
$ yarn test -- -m 'Test title RegExp' // run test with matching title