In my JavaScript App (jQuery) I have two events that are fired asynchronously:
How do I "sync" these two async events in order to "call a method" only after these two events fired.
I would need an approach that works for multiple events also.
Use deferred objects.
var deferredObj1 = $.Deferred(),
deferredObj2 = $.Deferred();
$.when(deferredObj1,deferredObj2).done(function(){
console.log("They are both done!");
});
// inside the Event1 handler:
deferredObj1.resolve();
// inside the Event2 handler:
deferredObj2.resolve();
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