The Deferred object was introduced in jQuery 1.5 as a chainable utility used to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function. Since then, it has been subject to discussions, some criticisms, and a lot of changes along the way.[1] This object can be used for many asynchronous operations, like Ajax requests and animations, but also with JavaScript timing functions. Together with the Promise object, it represents the jQuery implementation of promises.
The Promise object is created starting from a Deferred object or a jQuery object and possesses a subset of the methods of the Deferred object (always(), done(), fail(), state(), and then()). Deferred objects are typically used if you write your own function that deals with asynchronous callbacks. So, your function is the producer of the value and you want to prevent users from changing the state of the Deferred.
In lesson 10 we covered the utility functions that jQuery provides to work with Ajax requests, but for your convenience, we omitted to say that the returned value of those functions, which is a jqHXR object as you might remember, implements the Promise interface. For this reason they’re sometimes referred to as Promise-compatible objects, and you can call all the methods of regular Promise objects on them. This will allow you to write cleaner and more readable code.
Let’s now discuss how to work with Deferreds and Promises.
Leave a Reply