promiseReduce.js 840 B

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