concatLimit.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _wrapAsync = require('./internal/wrapAsync.js');
  6. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  7. var _mapLimit = require('./mapLimit.js');
  8. var _mapLimit2 = _interopRequireDefault(_mapLimit);
  9. var _awaitify = require('./internal/awaitify.js');
  10. var _awaitify2 = _interopRequireDefault(_awaitify);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time.
  14. *
  15. * @name concatLimit
  16. * @static
  17. * @memberOf module:Collections
  18. * @method
  19. * @see [async.concat]{@link module:Collections.concat}
  20. * @category Collection
  21. * @alias flatMapLimit
  22. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  23. * @param {number} limit - The maximum number of async operations at a time.
  24. * @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
  25. * which should use an array as its result. Invoked with (item, callback).
  26. * @param {Function} [callback] - A callback which is called after all the
  27. * `iteratee` functions have finished, or an error occurs. Results is an array
  28. * containing the concatenated results of the `iteratee` function. Invoked with
  29. * (err, results).
  30. * @returns A Promise, if no callback is passed
  31. */
  32. function concatLimit(coll, limit, iteratee, callback) {
  33. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  34. return (0, _mapLimit2.default)(coll, limit, (val, iterCb) => {
  35. _iteratee(val, (err, ...args) => {
  36. if (err) return iterCb(err);
  37. return iterCb(err, args);
  38. });
  39. }, (err, mapResults) => {
  40. var result = [];
  41. for (var i = 0; i < mapResults.length; i++) {
  42. if (mapResults[i]) {
  43. result = result.concat(...mapResults[i]);
  44. }
  45. }
  46. return callback(err, result);
  47. });
  48. }
  49. exports.default = (0, _awaitify2.default)(concatLimit, 4);
  50. module.exports = exports['default'];