Skip to content

Commit 58dca5a

Browse files
author
Caolan McMahon
committed
Conflicts: dist/async.min.js
2 parents aa837d7 + 9e116c4 commit 58dca5a

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,10 @@ __Example__
738738
Determines the best order for running functions based on their requirements.
739739
Each function can optionally depend on other functions being completed first,
740740
and each function is run as soon as its requirements are satisfied. If any of
741-
the functions pass and error to their callback, that function will not complete
741+
the functions pass an error to their callback, that function will not complete
742742
(so any other functions depending on it will not run) and the main callback
743-
will be called immediately with the error.
743+
will be called immediately with the error. Functions also receive an object
744+
containing the results of functions on which they depend.
744745

745746
__Arguments__
746747

@@ -764,9 +765,11 @@ __Example__
764765
write_file: ['get_data', 'make_folder', function(callback){
765766
// once there is some data and the directory exists,
766767
// write the data to a file in the directory
768+
callback(null, filename);
767769
}],
768-
email_link: ['write_file', function(callback){
770+
email_link: ['write_file', function(callback, results){
769771
// once the file is written let's email a link to it...
772+
// results.write_file contains the filename returned by write_file.
770773
}]
771774
});
772775

dist/async.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/async.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361
return callback(null);
362362
}
363363

364-
var completed = [];
364+
var results = {};
365365

366366
var listeners = [];
367367
var addListener = function (fn) {
@@ -382,8 +382,8 @@
382382
};
383383

384384
addListener(function () {
385-
if (completed.length === keys.length) {
386-
callback(null);
385+
if (_keys(results).length === keys.length) {
386+
callback(null, results);
387387
}
388388
});
389389

@@ -396,24 +396,28 @@
396396
callback = function () {};
397397
}
398398
else {
399-
completed.push(k);
399+
var args = Array.prototype.slice.call(arguments, 1);
400+
if (args.length <= 1) {
401+
args = args[0];
402+
}
403+
results[k] = args;
400404
taskComplete();
401405
}
402406
};
403407
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
404408
var ready = function () {
405409
return _reduce(requires, function (a, x) {
406-
return (a && _indexOf(completed, x) !== -1);
410+
return (a && results.hasOwnProperty(x));
407411
}, true);
408412
};
409413
if (ready()) {
410-
task[task.length - 1](taskCallback);
414+
task[task.length - 1](taskCallback, results);
411415
}
412416
else {
413417
var listener = function () {
414418
if (ready()) {
415419
removeListener(listener);
416-
task[task.length - 1](taskCallback);
420+
task[task.length - 1](taskCallback, results);
417421
}
418422
};
419423
addListener(listener);

test/test-async.js

+36
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ exports['auto'] = function(test){
3232
});
3333
};
3434

35+
exports['auto results'] = function(test){
36+
var callOrder = [];
37+
async.auto({
38+
task1: ['task2', function(callback, results){
39+
test.same(results.task2, 'task2');
40+
setTimeout(function(){
41+
callOrder.push('task1');
42+
callback(null, 'task1a', 'task1b');
43+
}, 25);
44+
}],
45+
task2: function(callback){
46+
setTimeout(function(){
47+
callOrder.push('task2');
48+
callback(null, 'task2');
49+
}, 50);
50+
},
51+
task3: ['task2', function(callback, results){
52+
test.same(results.task2, 'task2');
53+
callOrder.push('task3');
54+
callback(null);
55+
}],
56+
task4: ['task1', 'task2', function(callback, results){
57+
test.same(results.task1, ['task1a','task1b']);
58+
test.same(results.task2, 'task2');
59+
callOrder.push('task4');
60+
callback(null, 'task4');
61+
}]
62+
},
63+
function(err, results){
64+
test.same(callOrder, ['task2','task3','task1','task4']);
65+
test.same(results, {task1: ['task1a','task1b'], task2: 'task2', task3: undefined, task4: 'task4'});
66+
test.done();
67+
});
68+
};
69+
70+
3571
exports['auto empty object'] = function(test){
3672
async.auto({}, function(err){
3773
test.done();

0 commit comments

Comments
 (0)