[Pkg-javascript-commits] [node-async] 12/480: make callbacks optional

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 ee28653bd7f8bfbfd169f89cf77f9d21be1fd2c1
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Thu May 20 17:42:35 2010 +0100

    make callbacks optional
---
 lib/async.js       | 25 +++++++++++--------
 test/test-async.js | 73 ++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 75 insertions(+), 23 deletions(-)

diff --git a/lib/async.js b/lib/async.js
index 546f00d..1aecbce 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -2,6 +2,7 @@ var events = require('events');
 
 
 exports.auto = function(tasks, callback){
+    callback = callback || function(){};
     var keys = Object.keys(tasks);
     if(!keys.length) return callback(null);
 
@@ -38,32 +39,36 @@ exports.auto = function(tasks, callback){
     });
 };
 
-exports.waterfall = function(arr){
+exports.waterfall = function(tasks, callback){
+    callback = callback || function(){};
     var wrapIterator = function(iterator){
         return function(){
             var args = Array.prototype.slice.call(arguments);
             var next = iterator.next();
             if(next) args.push(wrapIterator(next));
+            else     args.push(callback);
             process.nextTick(function(){iterator.apply(null, args)});
         };
     };
-    wrapIterator(exports.iterator(arr))();
+    wrapIterator(exports.iterator(tasks))();
 };
 
-exports.parallel = function(arr, callback){
+exports.parallel = function(tasks, callback){
+    callback = callback || function(){};
     var results = [];
-    arr.forEach(function(fn){
+    tasks.forEach(function(fn){
         fn(function(){
             var args = Array.prototype.slice.call(arguments);
             results.push((args.length > 1) ? args: args[0]);
-            if(results.length == arr.length){
+            if(results.length == tasks.length){
                 callback(results);
             }
         });
     });
 };
 
-exports.series = function(arr, callback){
+exports.series = function(tasks, callback){
+    callback = callback || function(){};
     var results = [];
     var saveArgs = function(fn){
         return function(){
@@ -79,17 +84,17 @@ exports.series = function(arr, callback){
             else iterator(saveArgs(function(){callback(results.slice(1));}));
         });
     };
-    wrapIterator(exports.iterator(arr))();
+    wrapIterator(exports.iterator(tasks))();
 };
 
-exports.iterator = function(arr){
+exports.iterator = function(tasks){
     var makeCallback = function(index){
         var fn = function(){
-            arr[index].apply(null, arguments);
+            tasks[index].apply(null, arguments);
             return fn.next();
         }
         fn.next = function(){
-            return (index < arr.length-1)? makeCallback(index+1): undefined;
+            return (index < tasks.length-1)? makeCallback(index+1): undefined;
         }
         return fn;
     };
diff --git a/test/test-async.js b/test/test-async.js
index a724d99..97e869f 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -9,13 +9,13 @@ exports.testAuto = function(test){
             setTimeout(function(){
                 callOrder.push('task1');
                 callback();
-            }, 100);
+            }, 50);
         }],
         task2: function(callback){
             setTimeout(function(){
                 callOrder.push('task2');
                 callback();
-            }, 200);
+            }, 100);
         },
         task3: ['task2', function(callback){
             callOrder.push('task3');
@@ -32,8 +32,15 @@ exports.testAuto = function(test){
     });
 };
 
+exports.testAutoNoCallback = function(test){
+    async.auto({
+        task1: function(callback){callback();},
+        task2: ['task1', function(callback){callback(); test.done();}],
+    });
+};
+
 exports.testWaterfall = function(test){
-    test.expect(7);
+    test.expect(6);
     var call_order = [];
     async.waterfall([
         function(callback){
@@ -44,7 +51,7 @@ exports.testWaterfall = function(test){
             call_order.push('fn2');
             test.equals(arg1, 'one');
             test.equals(arg2, 'two');
-            setTimeout(function(){callback(arg1, arg2, 'three');}, 100);
+            setTimeout(function(){callback(arg1, arg2, 'three');}, 50);
         },
         function(arg1, arg2, arg3, callback){
             call_order.push('fn3');
@@ -56,10 +63,17 @@ exports.testWaterfall = function(test){
         function(arg4, callback){
             call_order.push('fn4');
             test.same(call_order, ['fn1','fn2','fn3','fn4']);
-            // don't pass callback to last in waterfall chain
-            test.ok(callback === undefined);
-            test.done();
+            callback('test');
         }
+    ], function(){
+        test.done();
+    });
+};
+
+exports.testWaterfallNoCallback = function(test){
+    async.waterfall([
+        function(callback){callback();},
+        function(callback){callback(); test.done();},
     ]);
 };
 
@@ -114,13 +128,32 @@ exports.testWaterfallMultipleCallback = function(test){
 exports.testParallel = function(test){
     async.parallel([
         function(callback){
-            setTimeout(function(){callback(1);}, 100);
+            setTimeout(function(){callback(1);}, 50);
+        },
+        function(callback){
+            setTimeout(function(){callback(2);}, 100);
+        },
+        function(callback){
+            setTimeout(function(){callback(3,3);}, 25);
+        }
+    ],
+    function(results){
+        test.same(results, [[3,3],1,2]);
+        test.done();
+    });
+};
+
+
+exports.testParallel = function(test){
+    async.parallel([
+        function(callback){
+            setTimeout(function(){callback(1);}, 50);
         },
         function(callback){
-            setTimeout(function(){callback(2);}, 200);
+            setTimeout(function(){callback(2);}, 100);
         },
         function(callback){
-            setTimeout(function(){callback(3,3);}, 50);
+            setTimeout(function(){callback(3,3);}, 25);
         }
     ],
     function(results){
@@ -129,16 +162,23 @@ exports.testParallel = function(test){
     });
 };
 
+exports.testParallelNoCallback = function(test){
+    async.parallel([
+        function(callback){callback();},
+        function(callback){callback(); test.done();},
+    ]);
+};
+
 exports.testSeries = function(test){
     async.series([
         function(callback){
-            setTimeout(function(){callback(1);}, 100);
+            setTimeout(function(){callback(1);}, 50);
         },
         function(callback){
-            setTimeout(function(){callback(2);}, 200);
+            setTimeout(function(){callback(2);}, 100);
         },
         function(callback){
-            setTimeout(function(){callback(3,3);}, 50);
+            setTimeout(function(){callback(3,3);}, 25);
         }
     ],
     function(results){
@@ -147,6 +187,13 @@ exports.testSeries = function(test){
     });
 };
 
+exports.testSeriesNoCallback = function(test){
+    async.series([
+        function(callback){callback();},
+        function(callback){callback(); test.done();},
+    ]);
+};
+
 exports.testIterator = function(test){
     var call_order = [];
     var iterator = async.iterator([

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