diff options
author | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-15 13:25:45 +0000 |
---|---|---|
committer | Caolan McMahon <caolan@caolanmcmahon.com> | 2013-03-15 13:25:45 +0000 |
commit | 522d97f3d1d8a708265827ae23a66ccf30c5821c (patch) | |
tree | afcdceb85a9f38f7945c467a78e2ecc709bdb3e7 | |
parent | 05a0fb0e91756a54085cf45fdc50a363c7c34be6 (diff) | |
download | async-522d97f3d1d8a708265827ae23a66ccf30c5821c.tar.gz |
added applyEachSeries
-rw-r--r-- | README.md | 7 | ||||
-rwxr-xr-x | lib/async.js | 10 | ||||
-rwxr-xr-x | test/test-async.js | 30 |
3 files changed, 43 insertions, 4 deletions
@@ -905,6 +905,13 @@ async.each( --------------------------------------- +<a name="applyEachSeries" /> +### applyEachSeries(arr, iterator, callback) + +The same as applyEach only the functions are applied in series. + +--------------------------------------- + <a name="queue" /> ### queue(worker, concurrency) diff --git a/lib/async.js b/lib/async.js index 775d8ea..14d20cc 100755 --- a/lib/async.js +++ b/lib/async.js @@ -895,24 +895,26 @@ }; }; - async.applyEach = function (fns /*args...*/) { + var _applyEach = function (eachfn, fns /*args...*/) { var go = function () { var that = this; var args = Array.prototype.slice.call(arguments); var callback = args.pop(); - return async.each(fns, function (fn, cb) { + return eachfn(fns, function (fn, cb) { fn.apply(that, args.concat([cb])); }, callback); }; - if (arguments.length > 1) { - var args = Array.prototype.slice.call(arguments, 1); + if (arguments.length > 2) { + var args = Array.prototype.slice.call(arguments, 2); return go.apply(this, args); } else { return go; } }; + async.applyEach = doParallel(_applyEach); + async.applyEachSeries = doSeries(_applyEach); async.forever = function (fn, callback) { function next(err) { diff --git a/test/test-async.js b/test/test-async.js index 880c7b6..ff401e7 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -114,6 +114,36 @@ exports['applyEach'] = function (test) { }); }; +exports['applyEachSeries'] = function (test) { + test.expect(4); + var call_order = []; + var one = function (val, cb) { + test.equal(val, 5); + setTimeout(function () { + call_order.push('one'); + cb(null, 1); + }, 100); + }; + var two = function (val, cb) { + test.equal(val, 5); + setTimeout(function () { + call_order.push('two'); + cb(null, 2); + }, 50); + }; + var three = function (val, cb) { + test.equal(val, 5); + setTimeout(function () { + call_order.push('three'); + cb(null, 3); + }, 150); + }; + async.applyEachSeries([one, two, three], 5, function (err) { + test.same(call_order, ['one', 'two', 'three']); + test.done(); + }); +}; + exports['applyEach partial application'] = function (test) { test.expect(4); var call_order = []; |