UNPKG

798 BJavaScriptView Raw
1/**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * strict
8 */
9import isPromise from './isPromise';
10
11/**
12 * Similar to Array.prototype.reduce(), however the reducing callback may return
13 * a Promise, in which case reduction will continue after each promise resolves.
14 *
15 * If the callback does not return a Promise, then this function will also not
16 * return a Promise.
17 */
18export default function promiseReduce(values, callback, initialValue) {
19 return values.reduce(function (previous, value) {
20 return isPromise(previous) ? previous.then(function (resolved) {
21 return callback(resolved, value);
22 }) : callback(previous, value);
23 }, initialValue);
24}
\No newline at end of file