The jQuery concept of Deferred objects feels a bit more complicated than what I used to do with MochiKit‘s Async library. It could simply be the naming conventions that jQuery has chosen.
Deferred asynchronous calls can be used for more than AJAX!
Most of the jQuery examples is that the authors choose AJAX-based calls for examples on how the Deferred objects work. Rather than doing a series of setInterval and setTimeout things to check for animations to be completed or animations to finish I can use Deferred calls to reliably tell me that the variables that I want are ready to rock and roll.
Best tutorials so far
Deferred is kind of hard to explain but this is the simplest way I can try:
- You instantiate a Deferred object in a function that you want to be asynchronous. (AJAX calls are Deferred but not all Deferred are AJAX)
- a [promise()] allows other variables & functions subscribe to notifications that Deferred is done or failed
- Deferred.resolve() tells all subscribers of the Deferred that the Deferred is done executing
- Functions passed into Deferred.done() will be executed if
In various parts of your code you can allow other variables to wait for an outcome Deferred
doSomethingWhenDeferredIsDoneDoingWhatItNeedsToDo = d.promise()