summaryrefslogtreecommitdiff
path: root/lib/concatLimit.js
diff options
context:
space:
mode:
authorHubert Argasinski <argasinski.hubert@gmail.com>2017-06-23 00:21:33 -0400
committerGitHub <noreply@github.com>2017-06-23 00:21:33 -0400
commite9b28554fd139ae15cf53496dbfb966b13904858 (patch)
tree41cc14d34e5c528a614bfcb59a9021d6eb5922e6 /lib/concatLimit.js
parent2f14cac2757158cf0b12fc1252733a81d7b29310 (diff)
downloadasync-e9b28554fd139ae15cf53496dbfb966b13904858.tar.gz
preserve order, make variadic and handle falsy values in concat [fixes #1437] (#1436)
* preserve order in concat * remove comment * fix concatLimit linting * handle variadic and falsy results in concatLimit [fixes #1437] * fix tests (typos) * PR fixes
Diffstat (limited to 'lib/concatLimit.js')
-rw-r--r--lib/concatLimit.js30
1 files changed, 26 insertions, 4 deletions
diff --git a/lib/concatLimit.js b/lib/concatLimit.js
index a86fd74..4df03ea 100644
--- a/lib/concatLimit.js
+++ b/lib/concatLimit.js
@@ -1,5 +1,9 @@
-import doParallelLimit from './internal/doParallelLimit';
-import concat from './internal/concat';
+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.
@@ -14,9 +18,27 @@ import concat from './internal/concat';
* @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(err)] - A callback which is called after all the
+ * @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 doParallelLimit(concat);
+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);
+ });
+}