waterfall-exec
v2.0.2
Published
FIFO executor for array of Promises
Maintainers
Readme
waterfall-exec
Executes an array of Promise one after another.
Can also listen to the array for new items.
Install
$ yarn add waterfall-execUsage
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 |
booleanoptional causes main Promise is never resolved and Waterfall executes every new job added to thejobsarray. Default isfalse. - waitTimeout |
numberoptional timeout forwaitForItems. Ifundefined, there is no timeout andwaterfallwill tick forever. Default isundefined. - checkInterval |
numberoptional interval in miliseconds forwaitForItems. Default is1000. - onNewPromise |
functionoptional function to be called when new job starts. Accepts the stepPromiseas 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