Sebastopol: O’Reilly Media, 2013. — 132 p.
Orchestrating asynchronous function calls in JavaScript often leads to callback hell, but there is a reliable way to avoid this painful state of affairs. With this concise and simple guide, you'll learn how to use jQuery deferreds and promises, an elegant approach for managing asynchronous calls in both client and server applications.
This book contains 18 examples that use deferreds to solve progressively challenging real-world programming problems, along with 75 stimulating puzzles (and their solutions) that will help you understand how and when to use deferreds. You'll learn new tricks in a fun way, and become immersed in the practice of event-based programming.
Food for Thought.
Terminology: Deferreds and Promises.
Familiar Promises.
The jQuery Deferred API.
Consuming Promises.
Creating Deferreds.
Putting It All Together.
Deferred Dynamics.
Deprecated Promise Methods.
Deferred Recipes.
A Replacement for the setTimeout Function.
Messaging in Chrome Extensions.
Accessing Chrome Local Storage.
Running Promise-Returning Functions One by One.
A Promise Pool with an emptyPromise Method.
Displaying Google Maps.
Communicating with a Web Worker.
Using Web Sockets.
Automatically Retrying Failing Deferred Calls.
Memoization.
Short-Term Memoization of In-Progress Function Calls.
Streaming Promise Events.
Getting the First Result from a Set of Promises.
A Deferred Queue.
when2: An Improved jQuery.when
Timing Out Promises.
Controlling Your Own Destiny.
Deactivating a Promise.
More Time in the Mental Gymnasium.
Do You Really Understand jQuery Deferreds?
Promises/A+.
Promises Are First-Class Objects for Function Calls.
Asynchronous Data Structures.
Advantages of Deferreds.
Difficulties with Deferreds.
Further Reading.
Hints for Selected Challenges.
The Promises/A+ Specification.
Converting an ArrayBuffer to Base 64.