I have a jQuery script that starts two or more asynchronous operations at the same time, and I would like to execute a callback when all are complete. An example would be to start an animation to hide the current page, while I make an Ajax request to load the contents of the next page. Both the animation can finish first, as well as the request. After both are finished, I would like to display the new page.
I do not want the new page to be displayed before the previous page was fully hidden , even if it was before your content is ready . So the final callback should not be in either event.
How to do it? Preferably, I'm looking for a generic solution that works with two or more concurrent asynchronous events (it's rare, but I could for example have to do more than one concurrent Ajax request, and just run the callback after have all the results).
Note: I asked the question for JavaScript in general, but a specific jQuery solution (for example, something involving Deferred
s or Promise
s) would also be good in size. / p>