Reputation: 3
I need to wait for tasks of two API callback functions and when tasks are done I would like to receive a callback so I can use data from both functions.
I would like the function will be parallel executed ... I read a lot of questions about async.parallel but I can't find a way to use these functions with async.
Of course async is only an example, any other way will be fine if it works.
I'm sorry for my bad english, I hope the code is easier to understand.
Thank you
var dataFunctionA = "";
var dataFunctionB = "";
var callbackFunctionA = function(err, response, data) {
// do some work with data
dataFunctionA = "Hello";
}
var callbackFunctionB = function(err, response, data) {
// do some work with data
dataFunctionB = " World!";
}
function printHelloWorld(){
console.write(dataFunctionA + dataFunctionB);
}
APIClient.functionA(paramA, callbackFunctionA);
APIClient.functionB(paramB, callbackFunctionB);
// need to wait for the two callbacks
printHelloWorld();
Upvotes: 0
Views: 1094
Reputation: 3664
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>
Upvotes: 0
Reputation: 4604
If you don't want to use promises you could do something like:
var dataFunctionA = "";
var dataFunctionB = "";
var dataFunctionCount = 2;
function checkDone() {
dataFunctionCount--;
if(dataFunctionCount === 0) {
// need to wait for the two callbacks
printHelloWorld();
}
}
var callbackFunctionA = function(err, response, data) {
// do some work with data
dataFunctionA = "Hello";
checkDone();
}
var callbackFunctionB = function(err, response, data) {
// do some work with data
dataFunctionB = " World!";
checkDone();
}
function printHelloWorld(){
console.write(dataFunctionA + dataFunctionB);
}
APIClient.functionA(paramA, callbackFunctionA);
APIClient.functionB(paramB, callbackFunctionB);
Upvotes: 1