I’m looking for a Thread.Join() type thing. Lets say I trigger 4 events in javascript. I want to execute code but only after all 4 callbacks have been completed. How do I do that? The only way I can think of is checking if any the 4 callbacks are in process via global var in which case i ignore or throw exception. But otherwise do something like globalVar++ in each callback and call IsItDone() at the end of each callback which checks if the globalVar is == 4.
But.. I kind of dont like it especially since I need to use a global var.
If you don’t mind adding jQuery to your site you can use its “deferred object” capability.
Each async function (include jQuery’s built-in AJAX functions) can return a “promise” to return something later.
To achieve what you’re asking for you can then call:
If you prefer to have an array
pcontaining those promises instead, call$.whenlike this instead: