Skip to content

Commit

Permalink
improve async.series to pass previous results to tasks
Browse files Browse the repository at this point in the history
Related to issue caolan#957
  • Loading branch information
alexpusch committed Nov 21, 2015
1 parent b0797ca commit 33398c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 14 additions & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,20 @@
};

async.series = function(tasks, callback) {
_parallel(async.eachOfSeries, tasks, callback);
callback = callback || noop;
var results = _isArrayLike(tasks) ? [] : {};

async.eachOfSeries(tasks, function (task, key, callback) {
task(_restParam(function (err, args) {
if (args.length <= 1) {
args = args[0];
}
results[key] = args;
callback(err);
}), results);
}, function (err) {
callback(err, results);
});
};

async.iterator = function (tasks) {
Expand Down
7 changes: 5 additions & 2 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,13 +1112,16 @@ exports['series'] = {
callback(null, 1);
}, 25);
},
function(callback){
function(callback, results){
test.ok(results[0], 1);
setTimeout(function(){
call_order.push(2);
callback(null, 2);
}, 50);
},
function(callback){
function(callback, results){
test.ok(results[0], 1);
test.ok(results[1], 2);
setTimeout(function(){
call_order.push(3);
callback(null, 3,3);
Expand Down

0 comments on commit 33398c3

Please sign in to comment.