promise-async-flow

0.2.0 • Public • Published

promise-async-flow

lightly async flow control based on Promise

Installing and use

$ npm install promise-async-flow --save

then

// 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
<script type="dist/async-flow.js"></script>

API

Parallel

parallel(
  tasks: Iterable<Task:() => Promise<any> | any>,
  concurrency?: int = Infinity
): Promise<Array<any>>

Run each task in parallel and resolve with an array of results.

asyncFlow.parallel([
  () => 1,
  () => Promise.resolve(2),
  async () => (1 + await Promise.resolve(3))
]).then(console.log);
// => [ 1, 2, 4 ]

Settled

settled(
  tasks: Iterable<Task:() => Promise<any> | any>,
  concurrency?: int = Infinity
): Promise<Array<settlementobject>>

Run each task in parallel and resolve with an array of objects that each describe the outcome of each promise.

asyncFlow.settled([
  () => 1,
  () => Promise.reject(2),
  async () => { throw 3 }
]).then(console.log);
// => 
// [
//  { status: 'fulfilled', value: 1 },
//  { status: 'rejected',  reason: 2 }
//  { status: 'rejected',  reason: 3 }
// ]

Series

series(
  tasks: Iterable<Task:() => Promise<any> | any>
): Promise<Array<any>>

Run each task in series and resolve with an array of results.

asyncFlow.series([
  () => 1,
  () => Promise.resolve(2),
  async () => (1 + await Promise.resolve(3))
]).then(console.log);
// => [ 1, 2, 4 ]

Waterfall

waterfall(
  tasks: Iterable<Task:() => Promise<any> | any>,
  initialValue?: any = undefined
): Promise<any>

Run each task in series, could pass the return to the next task and resolve with the last of result.

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

npm run test

License

MIT

Install

npm i promise-async-flow

DownloadsWeekly Downloads

3

Version

0.2.0

License

MIT

Unpacked Size

16.4 kB

Total Files

19

Last publish

Collaborators

  • takumiao13