As you know, async.parallel, defined with a such code:
async.parallel([
function (callback) {
callback(err, objects);
},
function (callback) {
callback(err, status);
},
function (callback) {
callback(err, status);
},
], function (err, results) {
//smth with results[N] array...
});
performs all the tasks all together parallel. However, I need the callback result of first function (objects, to be exact) to be avialable in 2nd and 3rd functions. In other words, first step – 1st function, second – ( 2rd + 3rd parallel with results of the 1st one). async.waterfall seems to be a bad idea 'cause:
Any ideas? Thanks!
You need both waterfall and parallel.
function thing1(callback) {...callback(null, thing1Result);}
function thing2A(thing1Result, callback) {...}
function thing2B(thing1Result, callback) {...}
function thing2(thing1Result, callback) {
async.parallel([
async.apply(thing2A, thing1Result),
async.apply(thing2B, thing1Result)
], callback);
}
async.waterfall([thing1, thing2], function (error) {
//all done
});
There's no need to use async. With async you are basically black-boxing your app. Because I don't like the magic for easy tasks, vanilla js:
var f1 = function (cb){
...
cb (null, "result from f1"); //null error
};
var f2 = function (resultFromF1, cb){
...
cb (null); //null error
};
var f3 = function (resultFromF1, cb){
...
cb (null); //null error
};
var main = function (cb){
f1 (function (error, resultFromF1){
if (error) return cb ([error]);
var errors = [];
var remaining = 2;
var finish = function (error){
if (error) errors.push (error);
if (!--remaining){
//f2 and f3 have finished
cb (errors.length ? errors : null);
}
};
f2 (resultFromF1, finish);
f3 (resultFromF1, finish);
});
};
main (function (errors){
if (errors) return handleError (errors); //errors is an array
...
});