Chapter 2. The jQuery Deferred API
There are different levels at which you can learn about jQuery deferreds, and these each give a different perspective.
At the lowest level there is the JavaScript source, the jQuery deferred.js and callbacks.js files. Reading the source is very informative, but itâs definitely not the simplest JavaScript to understand! Besides being challenging to follow (jQuery is optimized for code size and execution speed, not readability), the source also doesnât tell you what deferreds are for or how to use them. From reading the source, itâs not even clear what the methods available on deferreds might be.
Next, thereâs the official jQuery documentation for the
Deferred object,
jQuery.when
(which weâll refer to as $.when
from
now on), and the .promise()
function for DOM
element collections. The API documentation tells you what methods are
available, what their arguments are, methods that are deprecated or that
have changed between versions, etc. Youâll want to read the official
documentation closely and will probably return to it many times as you
become increasingly fluent with deferreds.
A further level is a proposal (see Promises/A+) for standardizing the behavior of promises across JavaScript libraries. While not directly associated with jQueryâs deferreds and promises, it illustrates the guidance that informed the implementation of the API.
Whatâs missing is a higher-level discussion that explains the API and the dynamics of deferreds. Thatâs ...
Get Learning jQuery Deferreds now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.