flatMapLimit.js
1 'use strict'; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 7 var _wrapAsync = require('./internal/wrapAsync'); 8 9 var _wrapAsync2 = _interopRequireDefault(_wrapAsync); 10 11 var _mapLimit = require('./mapLimit'); 12 13 var _mapLimit2 = _interopRequireDefault(_mapLimit); 14 15 var _awaitify = require('./internal/awaitify'); 16 17 var _awaitify2 = _interopRequireDefault(_awaitify); 18 19 function _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 */ 41 function 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 } 59 exports.default = (0, _awaitify2.default)(concatLimit, 4); 60 module.exports = exports['default'];