await-reduce
Version:
Resolve and reduce an array of promises
85 lines (70 loc) • 3.52 kB
JavaScript
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
/**
* @typedef Reducer
* @type {Function}
* @description Function to execute on each element in the array, taking four arguments
* @param {Any} accumulator The accumulator accumulates the callback's return values
* @param {Any} value The current element being processed in the array.
* @param {Number} index The index of the current element being processed in the array. Starts at index 0, if an initial value is provided, and at index 1 otherwise.
* @param {Array} array The array reduce() was called upon.
*/
/**
* [reduce description]
* @param {Array} array
* @param {Reducer} reducer
* @param {Any} initial Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce() on an empty array without an initial value is an error.
* @return {Any}
*/
module.exports =
/*#__PURE__*/
function () {
var _reduce = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(array, reducer, initial) {
var results, provided, index, accumulator, value;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (Array.isArray(array)) {
_context.next = 2;
break;
}
throw new TypeError("reduce function expects first argument to be an array. instead got ".concat(_typeof(array)));
case 2:
_context.next = 4;
return Promise.all(array);
case 4:
results = _context.sent;
provided = typeof initial !== 'undefined';
index = provided ? 0 : 1;
accumulator = provided ? initial : results[0];
case 8:
if (!(index < results.length)) {
_context.next = 16;
break;
}
value = results[index];
_context.next = 12;
return reducer(accumulator, value, index, results);
case 12:
accumulator = _context.sent;
index++;
_context.next = 8;
break;
case 16:
return _context.abrupt("return", accumulator);
case 17:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return function reduce(_x, _x2, _x3) {
return _reduce.apply(this, arguments);
};
}();
;