import noop from 'lodash/noop'; import wrapAsync from './internal/wrapAsync'; import slice from './internal/slice'; import mapLimit from './mapLimit'; var _concat = Array.prototype.concat; /** * The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time. * * @name concatLimit * @static * @memberOf module:Collections * @method * @see [async.concat]{@link module:Collections.concat} * @category Collection * @param {Array|Iterable|Object} coll - A collection to iterate over. * @param {number} limit - The maximum number of async operations at a time. * @param {AsyncFunction} iteratee - A function to apply to each item in `coll`, * which should use an array as its result. Invoked with (item, callback). * @param {Function} [callback] - A callback which is called after all the * `iteratee` functions have finished, or an error occurs. Results is an array * containing the concatenated results of the `iteratee` function. Invoked with * (err, results). */ export default function(coll, limit, iteratee, callback) { callback = callback || noop; var _iteratee = wrapAsync(iteratee); mapLimit(coll, limit, function(val, callback) { _iteratee(val, function(err /*, ...args*/) { if (err) return callback(err); return callback(null, slice(arguments, 1)); }); }, function(err, mapResults) { var result = []; for (var i = 0; i < mapResults.length; i++) { if (mapResults[i]) { result = _concat.apply(result, mapResults[i]); } } return callback(err, result); }); }