Permalink
Browse files

added error handling to parallel, series and auto functions

  • Loading branch information...
caolan committed May 26, 2010
1 parent ee28653 commit d7d0349a8cbd1073104b69e94f3c863edff075c4
Showing with 72 additions and 32 deletions.
  1. +34 −13 lib/async.js
  2. +38 −19 test/test-async.js
View
@@ -16,9 +16,16 @@ exports.auto = function(tasks, callback){
keys.forEach(function(k){
var task = (tasks[k] instanceof Function)? [tasks[k]]: tasks[k];
- var taskCallback = function(){
- completed.push(k);
- emitter.emit('taskComplete');
+ var taskCallback = function(err){
+ if(err){
+ callback(err);
+ // stop subsequent errors hitting the callback multiple times
+ callback = function(){};
+ }
+ else {
+ completed.push(k);
+ emitter.emit('taskComplete');
+ }
};
var requires = task.slice(0, Math.abs(task.length-1)) || [];
var ready = function(){
@@ -57,11 +64,17 @@ exports.parallel = function(tasks, callback){
callback = callback || function(){};
var results = [];
tasks.forEach(function(fn){
- fn(function(){
- var args = Array.prototype.slice.call(arguments);
- results.push((args.length > 1) ? args: args[0]);
- if(results.length == tasks.length){
- callback(results);
+ fn(function(err){
+ if(err){
+ callback(err);
+ callback = function(){};
+ }
+ else {
+ var args = Array.prototype.slice.call(arguments, 1);
+ results.push((args.length > 1) ? args: args[0]);
+ if(results.length == tasks.length){
+ callback(null, results);
+ }
}
});
});
@@ -71,17 +84,25 @@ exports.series = function(tasks, callback){
callback = callback || function(){};
var results = [];
var saveArgs = function(fn){
- return function(){
- var args = Array.prototype.slice.call(arguments);
- results.push((args.length > 1) ? args: args[0]);
- fn.apply(null, args);
+ return function(err){
+ if(err){
+ callback(err);
+ callback = function(){};
+ }
+ else {
+ var args = Array.prototype.slice.call(arguments, 1);
+ results.push((args.length > 1) ? args: args[0]);
+ fn.apply(null, args);
+ }
}
};
var wrapIterator = function(iterator){
return saveArgs(function(){
var next = iterator.next();
if(next) iterator(wrapIterator(iterator.next()));
- else iterator(saveArgs(function(){callback(results.slice(1));}));
+ else iterator(saveArgs(function(){
+ callback(null, results.slice(1));
+ }));
});
};
wrapIterator(exports.iterator(tasks))();
View
@@ -32,6 +32,26 @@ exports.testAuto = function(test){
});
};
+exports.testAutoError = function(test){
+ test.expect(1);
+ async.auto({
+ task1: function(callback){
+ callback('testerror');
+ },
+ task2: ['task1', function(callback){
+ test.ok(false, 'task2 should not be called');
+ callback();
+ }],
+ task3: function(callback){
+ callback('testerror2');
+ },
+ },
+ function(err){
+ test.equals(err, 'testerror');
+ });
+ setTimeout(test.done, 200);
+};
+
exports.testAutoNoCallback = function(test){
async.auto({
task1: function(callback){callback();},
@@ -65,7 +85,7 @@ exports.testWaterfall = function(test){
test.same(call_order, ['fn1','fn2','fn3','fn4']);
callback('test');
}
- ], function(){
+ ], function(err){
test.done();
});
};
@@ -125,41 +145,39 @@ exports.testWaterfallMultipleCallback = function(test){
async.waterfall(arr);
};
+
exports.testParallel = function(test){
async.parallel([
function(callback){
- setTimeout(function(){callback(1);}, 50);
+ setTimeout(function(){callback(null, 1);}, 50);
},
function(callback){
- setTimeout(function(){callback(2);}, 100);
+ setTimeout(function(){callback(null, 2);}, 100);
},
function(callback){
- setTimeout(function(){callback(3,3);}, 25);
+ setTimeout(function(){callback(null, 3,3);}, 25);
}
],
- function(results){
+ function(err, results){
+ test.equals(err, null);
test.same(results, [[3,3],1,2]);
test.done();
});
};
-
-exports.testParallel = function(test){
+exports.testParallelError = function(test){
async.parallel([
function(callback){
- setTimeout(function(){callback(1);}, 50);
+ callback('error', 1);
},
function(callback){
- setTimeout(function(){callback(2);}, 100);
- },
- function(callback){
- setTimeout(function(){callback(3,3);}, 25);
+ callback('error2', 2);
}
],
- function(results){
- test.same(results, [[3,3],1,2]);
- test.done();
+ function(err, results){
+ test.equals(err, 'error');
});
+ setTimeout(test.done, 200);
};
exports.testParallelNoCallback = function(test){
@@ -172,16 +190,17 @@ exports.testParallelNoCallback = function(test){
exports.testSeries = function(test){
async.series([
function(callback){
- setTimeout(function(){callback(1);}, 50);
+ setTimeout(function(){callback(null, 1);}, 50);
},
function(callback){
- setTimeout(function(){callback(2);}, 100);
+ setTimeout(function(){callback(null, 2);}, 100);
},
function(callback){
- setTimeout(function(){callback(3,3);}, 25);
+ setTimeout(function(){callback(null, 3,3);}, 25);
}
],
- function(results){
+ function(err, results){
+ test.equals(err, null);
test.same(results, [1,2,[3,3]]);
test.done();
});

0 comments on commit d7d0349

Please sign in to comment.