Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added applyEachSeries

  • Loading branch information...
commit 522d97f3d1d8a708265827ae23a66ccf30c5821c 1 parent 05a0fb0
@caolan authored
Showing with 43 additions and 4 deletions.
  1. +7 −0 README.md
  2. +6 −4 lib/async.js
  3. +30 −0 test/test-async.js
View
7 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)
View
10 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) {
View
30 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 = [];
Please sign in to comment.
Something went wrong with that request. Please try again.