promise.extra
Promise.series
and Promise.waterfall
with vanilla es Promise
/thenable
Install
$ npm i promise.extra
Usage
// The reducer could even returns non-promise values
series(tasks, reducer?): Promise
- tasks
Array<Task>
an array of tasks- Task
any
each of the tasks could be anything.
- Task
- reducer
?ReducerFunction
the reducer which will process eachtask
and returns either aPromise
or any value.
Always returns a Promise
.
- prev The result value of the last task which processed by the
reducer
. If the return value of the last reducer is anPromise
,prev
will be the value inside the promise. - task The current task.
- index The index of task
- tasks Just the
tasks
argument of functionseries
The default value of reducer
is:
currentValue
currentValue
is one of the Task
s, and each Task
could be a Promise
or a normal JavaScript variable or object.
If you want each of the Task
s to be a factory function that returns a Promise
or normal values, and execute each value inside the reducer, just define the reducer as:
{ // If `series` is invoked by `series.call(this, ...args)`, // reducer could share the `this` object. return factory}
waterfall(tasks, initValue, reducer?)
- tasks
Array<PromiseFactory>
- PromiseFactory
Function(x): Promise
a factory function which receives a parameter and returns aPromise
- PromiseFactory
- initValue
any | undefined
optional initial value which will be passed into the first factory function. - reducer
Always returns Promise
.
reduce(tasks, reducer, initValue)
- tasks
- reducer
Function(prev, factory, currentIndex, tasks): Promise
The reducer function - initValue
any | undefined
if no initial value is supplied,undefined
will be used.
Always returns Promise
findIndex(tasks, matcher, reducer?)
find(tasks, matcher, reducer?)
indexOf(tasks, value, reducer?)
some(tasks, reducer?)
every(tasks, reducer?)
Each of the methods is similar to the behavior of Array.prototype.<method>
.
factory(promise)
- promise
Promise|PromiseLike
Creates the new reduce
, series
, waterfall
with the Promise
which can be bluebird
, promise-faker
or something.
const series } =
Examples
reducer
Usage of const nickName = 'Steve' // Suppose there are two async functions to check the nickName
this
Object
The { if thisnumber < 10 thisnumber ++ return true if notThrow return false return Promise} series// Reject series// Promise.resolve<true> series// Promise.resolve<false>
Upgrade Guide
3.x -> 4.x
promise.extra
no longer supports node < 6 since 4.0.0
. For old node versions, require('promise.extra/legacy')
License
MIT