[Pkg-javascript-commits] [node-async] 07/480: added test for running waterfall callbacks async, and using callbacks multiple times

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:07 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 1ea1153989b371b993c1121164948524181db2df
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Sun May 16 16:03:22 2010 +0100

    added test for running waterfall callbacks async, and using callbacks multiple times
---
 lib/async.js       | 19 ++++++++++---------
 test/test-async.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/lib/async.js b/lib/async.js
index 732c386..3634bf2 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -50,15 +50,16 @@ exports.auto = function(tasks, callback){
 };
 
 exports.waterfall = function(arr){
-    var i = -1;
-    (function(){
-        i++;
-        if(i < arr.length){
-            var args = Array.prototype.slice.call(arguments);
-            if(i < arr.length-1){
-                args = args.concat(arguments.callee)
+    var makeCallback = function(index){
+        if(index < arr.length){
+            return function(){
+                var args = Array.prototype.slice.call(arguments);
+                if(index < arr.length-1){
+                    args = args.concat(makeCallback(index+1));
+                }
+                process.nextTick(function(){arr[index].apply(null, args);});
             }
-            process.nextTick(function(){arr[i].apply(null, args);});
         }
-    })();
+    };
+    makeCallback(0)();
 };
diff --git a/test/test-async.js b/test/test-async.js
index cad8c64..b1cffdf 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -80,3 +80,51 @@ exports.testWaterfall = function(test){
         }
     ]);
 };
+
+exports.testWaterfallAsync = function(test){
+    var call_order = [];
+    async.waterfall([
+        function(callback){
+            call_order.push(1);
+            callback();
+            call_order.push(2);
+        },
+        function(callback){
+            call_order.push(3);
+            callback();
+        },
+        function(){
+            test.same(call_order, [1,2,3]);
+            test.done();
+        }
+    ]);
+};
+
+exports.testWaterfallMultipleCallback = function(test){
+    var call_order = [];
+    var arr = [
+        function(callback){
+            call_order.push(1);
+            // call the callback twice. this should call function 2 twice
+            callback('one', 'two');
+            callback('one', 'two');
+        },
+        function(arg1, arg2, callback){
+            call_order.push(2);
+            callback(arg1, arg2, 'three');
+        },
+        function(arg1, arg2, arg3, callback){
+            call_order.push(3);
+            callback('four');
+        },
+        function(arg4){
+            call_order.push(4);
+            arr[3] = function(){
+                call_order.push(4);
+                test.same(call_order, [1,2,2,3,3,4,4]);
+                test.done();
+            };
+        }
+    ];
+    async.waterfall(arr);
+};

-- 
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