I can't get how can I execute a callback after 'n' async functions are executed, example:
var after4AsyncOperation = function(){
//do something...
}
process.nextTick(function(){
//do stuff
})
process.nextTick(function(){
//do stuff
})
process.nextTick(function(){
//do stuff
})
process.nextTick(function(){
//do stuff
})
Is there a way to execute after4AsyncOperation
after the 4 async functions without have to write the function one inside the other, ex:
var after4AsyncOperation = function(){
//do something...
}
process.nextTick(function(){
//do stuff
process.nextTick(function(){
//do stuff
process.nextTick(function(){
//do stuff
process.nextTick(function(){
//do stuff
after4Asyncoperation();
})
})
})
})
function a() { // first function code here $(document). trigger('function_a_complete'); } function b() { // second function code here } $(document). bind('function_a_complete', b); Using this method, function 'b' can only execute AFTER function 'a', as the trigger only exists when function a is finished executing.
Using callbacks, we can begin to separate – with a callback, we can load the data and, when the asynchronous operation is done, run the callback function. It does keep them nicely separated and it does pass the data into the callback as a parameter.
What is a Callback? Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name 'call back'. More complexly put: In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.
I would suggest using an async library such as Caolan's Async. Then, you can use async.parallel
, async.series
, or async.auto
. auto
is the most flexible, but a bit slower than the others; choosing between parallel
and series
would require more information on what you're trying to do - your first example would be a parallel operation, but your second example is in series.
However, if you don't want to use a library, you can write something custom. I would do it as follows:
var numFuncs = 4;
var callback = function(){
numFuncs--;
if(numFuncs == 0) {
after4AsyncOperation();
}
}
var after4AsyncOperation = function(){
//do something...
}
process.nextTick(function(){
//do stuff
callback();
})
process.nextTick(function(){
//do stuff
callback();
})
process.nextTick(function(){
//do stuff
callback();
})
process.nextTick(function(){
//do stuff
callback();
})
This works by counting down from the number of times we expect the callback to be called (numFuncs
) and only calling after4AsyncOperation
when we've been called back the right number of times.
You can take advantage of the great async library that has this functionality and many other nice features. To accomplish specifically what you're looking to do, you can either use series:
https://github.com/caolan/async#series
or parallel:
https://github.com/caolan/async#parallel
The difference between the two is that series runs all the functions one after the next, while parallel runs all of them at the same time. In either case, there's a callback after they're all done which is what you seem to be trying to accomplish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With