UNPKG

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