diff options
author | Calvin French-Owen <calvin@segment.io> | 2012-11-20 17:44:49 -0800 |
---|---|---|
committer | Calvin French-Owen <calvin@segment.io> | 2012-11-20 17:44:49 -0800 |
commit | 49c7fd7d46e50444889dab174a785d87d2b70cd2 (patch) | |
tree | 1d32e75d2a55e3f7a41e025ef36463415359c9e0 /lib/async.js | |
parent | 96a7da519adc9e8cb3c187a2da4945f5edea71d3 (diff) | |
download | async-49c7fd7d46e50444889dab174a785d87d2b70cd2.tar.gz |
Adding mapLimit
Diffstat (limited to 'lib/async.js')
-rw-r--r-- | lib/async.js | 67 |
1 files changed, 40 insertions, 27 deletions
diff --git a/lib/async.js b/lib/async.js index bbbd05c..aeab289 100644 --- a/lib/async.js +++ b/lib/async.js @@ -118,40 +118,48 @@ }; async.forEachLimit = function (arr, limit, iterator, callback) { - callback = callback || function () {}; - if (!arr.length || limit <= 0) { - return callback(); - } - var completed = 0; - var started = 0; - var running = 0; + var fn = forEachLimit(limit); + fn.apply(null, [arr, iterator, callback]); + }; - (function replenish () { - if (completed === arr.length) { + var forEachLimit = function (limit) { + + return function (arr, iterator, callback) { + callback = callback || function () {}; + if (!arr.length || limit <= 0) { return callback(); } + var completed = 0; + var started = 0; + var running = 0; - while (running < limit && started < arr.length) { - started += 1; - running += 1; - iterator(arr[started - 1], function (err) { - if (err) { - callback(err); - callback = function () {}; - } - else { - completed += 1; - running -= 1; - if (completed === arr.length) { - callback(); + (function replenish () { + if (completed === arr.length) { + return callback(); + } + + while (running < limit && started < arr.length) { + started += 1; + running += 1; + iterator(arr[started - 1], function (err) { + if (err) { + callback(err); + callback = function () {}; } else { - replenish(); + completed += 1; + running -= 1; + if (completed === arr.length) { + callback(); + } + else { + replenish(); + } } - } - }); - } - })(); + }); + } + })(); + }; }; @@ -186,6 +194,11 @@ async.map = doParallel(_asyncMap); async.mapSeries = doSeries(_asyncMap); + async.mapLimit = function (arr, limit, iterator, callback) { + var fn = forEachLimit(limit); + return _asyncMap.apply(null, [fn, arr, iterator, callback]); + }; + // reduce only has a series version, as doing reduce in parallel won't // work in many situations. |