Chapter 13

Advanced Asynchronous Programming with jQuery Deferred

WHAT’S IN THIS CHAPTER?

  • The Promises/A Proposal
  • The jQuery Deferred Object

In this chapter, you learn about the jQuery Deferred object. $.Deferred, introduced in version 1.5, is a chainable utility object that provides fine-tuned control over the way callback functions are handled. For example, instead of merely having a single success method for your $.ajax calls, you can now define multiple callbacks that can be combined with precision and flexibility.

This chapter also introduces the CommonJS Promises/A proposal that fed the design of $.Deferred. Following that foundation, you learn about the jQuery implementation in detail.

Leveraging the power of $.Deferred will make some unpleasant coding patterns disappear. Monolithic success blocks, chock full of application logic, will be a thing of the past. Awkward timing with asynchronous requests and unmaintainable function calls buried at the end of other function calls can be put to bed with $.Deferred. $.Deferred pulls all of that logic up a level and gives it a clear structure that’s more maintainable, more flexible, and is, as is often the case with jQuery, a fun pattern to use.

BUILDING BLOCKS

$.Deferred is built on existing work and concepts from sources outside the jQuery project. To understand the feature in the fullest, it’s useful to look at those sources directly. This section will outline the origin of the pattern, which will ease you into an understanding ...

Get Professional jQuery now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.