[Pkg-javascript-commits] [node-async] 316/480: added applyEachSeries

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:37 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-async.

commit 522d97f3d1d8a708265827ae23a66ccf30c5821c
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Fri Mar 15 13:25:45 2013 +0000

    added applyEachSeries
---
 README.md          |  7 +++++++
 lib/async.js       | 10 ++++++----
 test/test-async.js | 30 ++++++++++++++++++++++++++++++
 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 = [];

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git



More information about the Pkg-javascript-commits mailing list