Consider a case wherein the callback value of the first function in waterfall needs to be used multiple times in subsequent functions to finally coalesce the results. In the following code, baseFn should return an array in the callback, which should then be processed element by element as indicated in subFn1 and subFn2. Thereafter, the return callbacks of subFn1 and subFn2 must be appended into a new array to produce the final result. I have trying to see, if there is a way to achieve this using async.series and async.apply, but haven't able to get much further. Please advise.
async.waterfall ([
function baseFn(baseCb) {
//a method that gets an array of elements
return (null, elementArray);
},
function subFn1(elemArr, subFnCb) {
var elem = elemArr[0];
//a method that returns processed element
return (null, proc_elem1);
},
function subFn2(elemArr,subFnCb) {
var elem = elemArr[1];
//a method that returns processed element
return (null, proc_elem2);
},
function aggOut() {}
], function getOutput (err, out) {
console.log(out);
})
Since subFn1() and subFn2() both depend on baseFn, it's best to use async.auto() because we can specify the dependency of subFn1 and subFn2 is baseFn(). Here is the code:
var async = require('async');
async.auto({
baseFn: function(autoCb) {
var elementArray = ['one', 'two'];
autoCb(null, elementArray);
},
subFn1: ['baseFn', function(results, autoCb) {
var elem = results.baseFn[0];
console.log('subFn1', elem);
var proc_elem1 = 'proc_elem1';
autoCb(null, proc_elem1);
}],
subFn2: ['baseFn', function(results, autoCb) {
var elem = results.baseFn[1];
console.log('subFn2', elem);
var proc_elem2 = 'proc_elem2';
autoCb(null, proc_elem2);
}]
}, function(err, results) {
console.log('results', results);
});
The output will look something like this (note that the order of subFn1 and subFn2 completion is not guaranteed since they run in parallel. However, the final 'results' object in the final callback will have all results):
subFn1 one
subFn2 two
results { baseFn: [ 'one', 'two' ],
subFn1: 'proc_elem1',
subFn2: 'proc_elem2' }
const baseFn = function(baseCb) {
// ...
return baseCb(elementArray);
}
baseFn(function(elemArr) {
// since subFn1 and subFn2 both need elemArr independently,
// lets use async.parallel here.
async.parallel([
function subFn1(elemArr, subFnCb) {
// ...
return subFnCb(null, proc_elem1);
}),
function subFn2(elemArr, subFnCb) {
// ...
return subFnCb(null, proc_elem2);
}),
], function(err, result) {
// async.parallel callback with proc_elem1 and 2 in result.
// you can now create your final result with elementArray
// and both proc_elem.
console.log([elemArr, result[0], result[1]]);
});
});