UNPKG

843 BJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = promiseReduce;
7
8var _isPromise = _interopRequireDefault(require("./isPromise.js"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12/**
13 * Similar to Array.prototype.reduce(), however the reducing callback may return
14 * a Promise, in which case reduction will continue after each promise resolves.
15 *
16 * If the callback does not return a Promise, then this function will also not
17 * return a Promise.
18 */
19function promiseReduce(values, callback, initialValue) {
20 return values.reduce(function (previous, value) {
21 return (0, _isPromise.default)(previous) ? previous.then(function (resolved) {
22 return callback(resolved, value);
23 }) : callback(previous, value);
24 }, initialValue);
25}