1 | import isPromise from './isPromise';
|
2 |
|
3 | /**
|
4 | * Similar to Array.prototype.reduce(), however the reducing callback may return
|
5 | * a Promise, in which case reduction will continue after each promise resolves.
|
6 | *
|
7 | * If the callback does not return a Promise, then this function will also not
|
8 | * return a Promise.
|
9 | */
|
10 | export default function promiseReduce(values, callback, initialValue) {
|
11 | return values.reduce(function (previous, value) {
|
12 | return isPromise(previous) ? previous.then(function (resolved) {
|
13 | return callback(resolved, value);
|
14 | }) : callback(previous, value);
|
15 | }, initialValue);
|
16 | }
|