Permalink
Browse files

added test for running waterfall callbacks async, and using callbacks…

… multiple times
  • Loading branch information...
1 parent 6470518 commit 1ea1153989b371b993c1121164948524181db2df @caolan committed May 16, 2010
Showing with 58 additions and 9 deletions.
  1. +10 −9 lib/async.js
  2. +48 −0 test/test-async.js
View
@@ -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)();
};
View
@@ -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);
+};

0 comments on commit 1ea1153

Please sign in to comment.