diff options
author | Alexander Early <aearly@fluid.com> | 2015-01-20 11:18:50 -0800 |
---|---|---|
committer | Alexander Early <aearly@fluid.com> | 2015-01-20 11:18:50 -0800 |
commit | 52dd10d744a3c2d82b3f29466462e569adc8de39 (patch) | |
tree | 732059951186dded902a4f399be8878499f96dec /lib | |
parent | 6d612c8bceff7a88ebcc263a139f336adec79e9e (diff) | |
download | async-52dd10d744a3c2d82b3f29466462e569adc8de39.tar.gz |
reordered functions, added aliases, simplified eachOfLimit logic
Diffstat (limited to 'lib')
-rw-r--r-- | lib/async.js | 104 |
1 files changed, 53 insertions, 51 deletions
diff --git a/lib/async.js b/lib/async.js index c99024b..185812f 100644 --- a/lib/async.js +++ b/lib/async.js @@ -171,7 +171,56 @@ }; async.forEachSeries = async.eachSeries; - async.forEachOf = function (object, iterator, callback) { + + async.eachLimit = function (arr, limit, iterator, callback) { + var fn = _eachLimit(limit); + fn.apply(null, [arr, iterator, callback]); + }; + async.forEachLimit = async.eachLimit; + + var _eachLimit = 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; + + (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 { + completed += 1; + running -= 1; + if (completed >= arr.length) { + callback(); + } + else { + replenish(); + } + } + }); + } + })(); + }; + }; + + + + async.forEachOf = async.eachOf = function (object, iterator, callback) { callback = callback || function () {}; var size = object.length || _keys(object).length; var completed = 0 @@ -193,7 +242,7 @@ }); }; - async.forEachOfSeries = function (obj, iterator, callback) { + async.forEachOfSeries = async.eachOfSeries = function (obj, iterator, callback) { callback = callback || function () {}; var keys = _keys(obj); var size = keys.length; @@ -230,56 +279,9 @@ }; - async.eachLimit = function (arr, limit, iterator, callback) { - var fn = _eachLimit(limit); - fn.apply(null, [arr, iterator, callback]); - }; - async.forEachLimit = async.eachLimit; - - var _eachLimit = 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; - - (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 { - completed += 1; - running -= 1; - if (completed >= arr.length) { - callback(); - } - else { - replenish(); - } - } - }); - } - })(); - }; - }; - - async.forEachOfLimit = function (obj, limit, iterator, callback) { - var fn = obj.constructor === Array ? _forEachOfLimit(limit) : _forEachOfLimit(limit); - fn.apply(null, [obj, iterator, callback]); + async.forEachOfLimit = async.eachOfLimit = function (obj, limit, iterator, callback) { + _forEachOfLimit(limit)(obj, iterator, callback); }; var _forEachOfLimit = function (limit) { |