lightly async flow control based on Promise
npm install promise-async-flowpromise-async-flow
==================
lightly async flow control based on Promise
bash
$ npm install promise-async-flow --save
`
then
`js
// cjs
const asyncFlow = require('promise-async-flow');
const { parallel } = require('promise-async-flow');
const parallel = require('promise-async-flow/parallel');
// esm
import asyncFlow from 'promise-async-flow'
import { parallel } from 'promise-async-flow'
import parallel from 'promise-async-flow/parallel'
// browser
`
API
- #parallel
- #settled
- #series
- #waterfall
$3
`js
parallel(
tasks: Iterable Promise | any>,
concurrency?: int = Infinity
): Promise>
`
Run each task in parallel and resolve with an array of results.
`js
asyncFlow.parallel([
() => 1,
() => Promise.resolve(2),
async () => (1 + await Promise.resolve(3))
]).then(console.log);
// => [ 1, 2, 4 ]
`
$3
`js
settled(
tasks: Iterable Promise | any>,
concurrency?: int = Infinity
): Promise>
`
Run each task in parallel and resolve with an array of objects that each describe the outcome of each promise.
`js
asyncFlow.settled([
() => 1,
() => Promise.reject(2),
async () => { throw 3 }
]).then(console.log);
// =>
// [
// { status: 'fulfilled', value: 1 },
// { status: 'rejected', reason: 2 }
// { status: 'rejected', reason: 3 }
// ]
`
$3
`js
series(
tasks: Iterable Promise | any>
): Promise>
`
Run each task in series and resolve with an array of results.
`js
asyncFlow.series([
() => 1,
() => Promise.resolve(2),
async () => (1 + await Promise.resolve(3))
]).then(console.log);
// => [ 1, 2, 4 ]
`
$3
`js
waterfall(
tasks: Iterable Promise | any>,
initialValue?: any = undefined
): Promise
`
Run each task in series, could pass the return to the next task and resolve with the last of result.
`js
asyncFlow.waterfall([
(arg) => {
console.log(arg); //=> 1
return arg + 1;
},
(arg) => {
console.log(arg); //=> 2
return arg * 2
},
(arg) => {
console.log(arg); //=> 4
return -1 * arg
}
], 1).then(console.log); //=> -4
`
NOTE:
- Task should be a promise-returning function.
- parallel series waterfall will reject if any task throw error or return a promise that rejects.
Test
`bash
npm run test
``