UNPKG

606 BJavaScriptView Raw
1import isPromise from "./isPromise.mjs";
2/**
3 * Similar to Array.prototype.reduce(), however the reducing callback may return
4 * a Promise, in which case reduction will continue after each promise resolves.
5 *
6 * If the callback does not return a Promise, then this function will also not
7 * return a Promise.
8 */
9
10export 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}