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.