Skip to content

Loading…

added support for accessing results of other tasks in async.auto #12

Closed
wants to merge 2 commits into from

5 participants

@thejh

As wanted in issue #7. Usage example:

async.auto({
    get_data: function(callback){
        // async code to get some data
        setTimeout(function() {
            callback(null, 'this is some data');
        }, 500);
    },
    make_folder: function(callback){
        // async code to create a directory to store a file in
        // this is run at the same time as getting the data
        setTimeout(function() {
            callback(null, 'blahfu123');
        }, 700);
    },
    write_file: ['get_data', 'make_folder', function(callback){
        setTimeout(function() {
            callback(null);
        }, 700);
    }],
    email_link: ['write_file', function(callback, results){
        console.log('put "'+results.get_data+'" into "'+results.make_folder+'"');
        setTimeout(function() {
            callback(null);
        }, 700);
    }]
});
@gatemedia

Exactly what i needed. Thanks for this addition.

@eirikurn

+1 for this feature

@eirikurn

I recommend adding the results object to the final callback. At least I have an equal need for that. That needs only three lines highlighted in this commit:

eirikurn/async@bb047d1

@thejh

Thanks, eirikurn, looks like a good idea.

@brianmaissy

This seems to have been implemented since the pull request was opened, so it should be closed.

@caolan caolan closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 8 additions and 6 deletions.
  1. +8 −6 lib/async.js
View
14 lib/async.js
@@ -319,9 +319,10 @@
async.auto = function (tasks, callback) {
callback = callback || function () {};
+ var results = {};
var keys = _keys(tasks);
if (!keys.length) {
- return callback(null);
+ return callback(null, results);
}
var completed = [];
@@ -346,20 +347,21 @@
addListener(function () {
if (completed.length === keys.length) {
- callback(null);
+ callback(null, results);
}
});
_forEach(keys, function (k) {
var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
- var taskCallback = function (err) {
+ var taskCallback = function (err, result) {
if (err) {
- callback(err);
+ callback(err, results);
// stop subsequent errors hitting callback multiple times
callback = function () {};
}
else {
completed.push(k);
+ results[k] = result;
taskComplete();
}
};
@@ -370,13 +372,13 @@
}, true);
};
if (ready()) {
- task[task.length - 1](taskCallback);
+ task[task.length - 1](taskCallback, results);
}
else {
var listener = function () {
if (ready()) {
removeListener(listener);
- task[task.length - 1](taskCallback);
+ task[task.length - 1](taskCallback, results);
}
};
addListener(listener);
Something went wrong with that request. Please try again.