Skip to content
Browse files

make callbacks optional

  • Loading branch information...
1 parent 75d506c commit ee28653bd7f8bfbfd169f89cf77f9d21be1fd2c1 @caolan committed May 20, 2010
Showing with 75 additions and 23 deletions.
  1. +15 −10 lib/async.js
  2. +60 −13 test/test-async.js
View
25 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;
};
View
73 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([

0 comments on commit ee28653

Please sign in to comment.
Something went wrong with that request. Please try again.