Permalink
Browse files

better support empty objects and arrays as arguments

  • Loading branch information...
1 parent e4305a3 commit c682809128a6f39b0cc79f7aaae2d97c903d188c @caolan committed Jun 12, 2010
Showing with 61 additions and 1 deletion.
  1. +4 −1 lib/async.js
  2. +57 −0 test/test-async.js
View
@@ -2,6 +2,7 @@ var events = require('events');
exports.forEach = function(arr, iterator, callback){
+ if(!arr.length) return callback();
var completed = 0;
arr.forEach(function(x){
iterator(x, function(err){
@@ -18,6 +19,7 @@ exports.forEach = function(arr, iterator, callback){
};
exports.forEachSeries = function(arr, iterator, callback){
+ if(!arr.length) return callback();
var completed = 0;
var iterate = function(){
iterator(arr[completed], function(err){
@@ -179,6 +181,7 @@ exports.auto = function(tasks, callback){
};
exports.waterfall = function(tasks, callback){
+ if(!tasks.length) return callback();
callback = callback || function(){};
var wrapIterator = function(iterator){
return function(err){
@@ -223,7 +226,7 @@ exports.series = function(tasks, callback){
exports.iterator = function(tasks){
var makeCallback = function(index){
var fn = function(){
- tasks[index].apply(null, arguments);
+ if(tasks.length) tasks[index].apply(null, arguments);
return fn.next();
}
fn.next = function(){
View
@@ -32,6 +32,12 @@ exports['auto'] = function(test){
});
};
+exports['auto empty object'] = function(test){
+ async.auto({}, function(err){
+ test.done();
+ });
+};
+
exports['auto error'] = function(test){
test.expect(1);
async.auto({
@@ -90,6 +96,12 @@ exports['waterfall'] = function(test){
});
};
+exports['waterfall empty array'] = function(test){
+ async.waterfall([], function(err){
+ test.done();
+ });
+};
+
exports['waterfall no callback'] = function(test){
async.waterfall([
function(callback){callback();},
@@ -192,6 +204,14 @@ exports['parallel'] = function(test){
});
};
+exports['parallel empty array'] = function(test){
+ async.parallel([], function(err, results){
+ test.equals(err, null);
+ test.same(results, []);
+ test.done();
+ });
+};
+
exports['parallel error'] = function(test){
async.parallel([
function(callback){
@@ -244,6 +264,14 @@ exports['series'] = function(test){
});
};
+exports['series empty array'] = function(test){
+ async.series([], function(err, results){
+ test.equals(err, null);
+ test.same(results, []);
+ test.done();
+ });
+};
+
exports['series error'] = function(test){
test.expect(1);
async.series([
@@ -294,6 +322,13 @@ exports['iterator'] = function(test){
test.done();
};
+exports['iterator empty array'] = function(test){
+ var iterator = async.iterator([]);
+ test.equals(iterator(), undefined);
+ test.equals(iterator.next(), undefined);
+ test.done();
+};
+
exports['iterator.next'] = function(test){
var call_order = [];
var iterator = async.iterator([
@@ -330,6 +365,17 @@ exports['forEach'] = function(test){
});
};
+exports['forEach empty array'] = function(test){
+ test.expect(1);
+ async.forEach([], function(x, callback){
+ test.ok(false, 'iterator should not be called');
+ callback();
+ }, function(err){
+ test.ok(true, 'should call callback');
+ });
+ setTimeout(test.done, 25);
+};
+
exports['forEach error'] = function(test){
test.expect(1);
async.forEach([1,2,3], function(x, callback){
@@ -353,6 +399,17 @@ exports['forEachSeries'] = function(test){
});
};
+exports['forEachSeries empty array'] = function(test){
+ test.expect(1);
+ async.forEachSeries([], function(x, callback){
+ test.ok(false, 'iterator should not be called');
+ callback();
+ }, function(err){
+ test.ok(true, 'should call callback');
+ });
+ setTimeout(test.done, 25);
+};
+
exports['forEachSeries error'] = function(test){
test.expect(2);
var call_order = [];

0 comments on commit c682809

Please sign in to comment.