summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolan McMahon <caolan@caolanmcmahon.com>2013-03-15 13:25:45 +0000
committerCaolan McMahon <caolan@caolanmcmahon.com>2013-03-15 13:25:45 +0000
commit522d97f3d1d8a708265827ae23a66ccf30c5821c (patch)
treeafcdceb85a9f38f7945c467a78e2ecc709bdb3e7
parent05a0fb0e91756a54085cf45fdc50a363c7c34be6 (diff)
downloadasync-522d97f3d1d8a708265827ae23a66ccf30c5821c.tar.gz
added applyEachSeries
-rw-r--r--README.md7
-rwxr-xr-xlib/async.js10
-rwxr-xr-xtest/test-async.js30
3 files changed, 43 insertions, 4 deletions
diff --git a/README.md b/README.md
index a695ae8..c5e8b51 100644
--- a/README.md
+++ b/README.md
@@ -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 = [];