Using the async.parallel you can achieve that by doing:
async.parallel({
functionA: APIClient.functionA.bind(null, paramA),
functionB: APIClient.functionB.bind(null, paramB)
}, function(error, results) {
processCallbacks(error, results);
});
And in your functions, after finishing data processing you should call the implicit parallel callback passed.
functionA(paramA, callback) {
// data processing
callback(null, resultFromProcessingParamA);
}
What you do here is passing an object of functions to the parallel. The key of the object here is used so that you can access the results array. For the example above, the result of functionA will be saved in results.functionA and so on.
The first parameter is null (due to .bind) and then you pass all the other needed params. The callback is passed automatically by the parallel between functions and the last callback is only called when all the functions finish.
Please run the following snippet to understand better.
function functionA (paramA, callback) {
$('body').append('<p> Parallel execution of function A and waiting for 5 seconds, parameter passed: ' + paramA + '</p>');
setTimeout(function(){
$('body').append('<p>processed functionA</p>');
callback(null, ++paramA);
}, 5000);
}
function functionB (paramB, callback) {
$('body').append('<p> Parallel execution of function B and waiting for 1 second, parameter passed: ' + paramB + '</p>');
setTimeout(function(){
$('body').append('<p>processed functionB</p>');
callback(null, ++paramB);
}, 1000);
}
function processCallbacks (results) {
$('body').append('<p> Processing results from two functions. ParamA now is:' + results.functionA + ' and paramB now is:' + results.functionB + '</p>');
}
async.parallel({
functionA: functionA.bind(null, 10),
functionB: functionB.bind(null, 2)
}, function(error, results) {
console.log(results);
if (!error) {
processCallbacks(results);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/1.5.0/async.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>