[Pkg-javascript-commits] [node-async] 13/480: added error handling to parallel, series and auto functions
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 d7d0349a8cbd1073104b69e94f3c863edff075c4
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Wed May 26 11:45:53 2010 +0100
added error handling to parallel, series and auto functions
---
lib/async.js | 47 +++++++++++++++++++++++++++++++-------------
test/test-async.js | 57 ++++++++++++++++++++++++++++++++++++------------------
2 files changed, 72 insertions(+), 32 deletions(-)
diff --git a/lib/async.js b/lib/async.js
index 1aecbce..3ba30dc 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -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))();
diff --git a/test/test-async.js b/test/test-async.js
index 97e869f..289ce4b 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -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();
});
--
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