diff options
author | Alexander Early <aearly@fluid.com> | 2015-05-30 21:56:38 -0700 |
---|---|---|
committer | Alexander Early <aearly@fluid.com> | 2015-05-30 21:56:38 -0700 |
commit | be0b6af6b0ed18ad9c48131a0565f106bd51b47d (patch) | |
tree | 315650af61007df9bf54b6a87b80dece20a022e4 /lib | |
parent | e32c48c03ffe733c66c419ca744d34d3d881db6d (diff) | |
download | async-be0b6af6b0ed18ad9c48131a0565f106bd51b47d.tar.gz |
simplified/optimized async.parallel()
Diffstat (limited to 'lib')
-rw-r--r-- | lib/async.js | 86 |
1 files changed, 14 insertions, 72 deletions
diff --git a/lib/async.js b/lib/async.js index 7c6c2c9..29dfb47 100644 --- a/lib/async.js +++ b/lib/async.js @@ -191,50 +191,6 @@ return _eachOfLimit(limit)(arr, _withoutIndex(iterator), callback); }; - function _eachLimit(limit) { - - return function (arr, iterator, callback) { - callback = callback || noop; - if (!arr.length || limit <= 0) { - return callback(null); - } - var completed = 0; - var started = 0; - var running = 0; - var errored = false; - - (function replenish () { - if (completed >= arr.length) { - return callback(null); - } - - while (running < limit && started < arr.length && !errored) { - started += 1; - running += 1; - iterator(arr[started - 1], function (err) { - if (err) { - callback(err); - errored = true; - callback = noop; - } - else { - completed += 1; - running -= 1; - if (completed >= arr.length) { - callback(null); - } - else { - replenish(); - } - } - }); - } - })(); - }; - } - - - async.forEachOf = async.eachOf = function (object, iterator, callback) { callback = callback || noop; @@ -700,42 +656,28 @@ function _parallel(eachfn, tasks, callback) { callback = callback || noop; - if (_isArray(tasks)) { - eachfn.map(tasks, function (fn, callback) { - if (fn) { - fn(function (err) { - var args = _baseSlice(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - callback.call(null, err, args); - }); + var results = _isArrayLike(tasks) ? [] : {}; + + eachfn(tasks, function (task, key, callback) { + task(function (err) { + var args = _baseSlice(arguments, 1); + if (args.length <= 1) { + args = args[0]; } - }, callback); - } - else { - var results = {}; - eachfn.each(_keys(tasks), function (k, callback) { - tasks[k](function (err) { - var args = _baseSlice(arguments, 1); - if (args.length <= 1) { - args = args[0]; - } - results[k] = args; - callback(err); - }); - }, function (err) { - callback(err, results); + results[key] = args; + callback(err); }); - } + }, function (err) { + callback(err, results); + }); } async.parallel = function (tasks, callback) { - _parallel({ map: async.map, each: async.each }, tasks, callback); + _parallel(async.eachOf, tasks, callback); }; async.parallelLimit = function(tasks, limit, callback) { - _parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback); + _parallel(_eachOfLimit(limit), tasks, callback); }; async.series = function (tasks, callback) { |