Learning jQuery Deferreds

Book description

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. Experienced JavaScript programmers will learn new tricks in a fun way, and become immersed in the practice of event-based programming.

  • Understand the logic behind creating deferreds and returning promises
  • Get a structured explanation of jQuery’s deferred API
  • Delve into the dynamics of using deferreds
  • Explore a broad collection of useful deferred recipes developed by the authors
  • Gain hands-on experience by solving challenges that accompany each recipe
  • Go deeper into deferreds: encounter novel abstractions and mind-bending use cases

Table of contents

  1. Preface
    1. About You
    2. Our Aims
    3. Challenges
    4. jQuery Deferreds
    5. Our JavaScript Coding Style, or Lack Thereof
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. How to Contact Us
    10. Acknowledgments
  2. 1. Introduction
    1. Food for Thought
    2. Terminology: Deferreds and Promises
    3. Familiar Promises
  3. 2. The jQuery Deferred API
    1. Consuming Promises
      1. More Terminology: Resolve, Reject and Progress
      2. done
      3. fail
      4. always
      5. progress
      6. promise
      7. then
      8. state
      9. when
    2. Creating Deferreds
      1. Construction
      2. resolve and resolveWith
      3. reject and rejectWith
      4. notify and notifyWith
    3. Putting It All Together
    4. Deferred Dynamics
    5. Deprecated Promise Methods
      1. isRejected and isResolved
      2. pipe
    6. Changes in the jQuery Deferred API
  4. 3. Deferred Recipes
    1. A Replacement for the setTimeout Function
      1. Challenges
    2. Messaging in Chrome Extensions
      1. Challenges
    3. Accessing Chrome Local Storage
      1. Challenges
    4. Running Promise-Returning Functions One by One
      1. Challenges
    5. A Promise Pool with an emptyPromise Method
      1. Creating a Promise Pool
      2. Using the Promise Pool
      3. Challenges
    6. Displaying Google Maps
      1. Challenges
    7. Communicating with a Web Worker
      1. The Web Worker Code
      2. Creating a Web Worker
      3. Using It
      4. Summary
      5. Challenges
    8. Using Web Sockets
      1. The Web Socket Server
      2. The Web Socket Client
      3. Challenges
    9. Automatically Retrying Failing Deferred Calls
      1. Challenges
    10. Memoization
      1. Discussion
      2. Avoiding the Dogpile Effect
      3. Challenges
    11. Short-Term Memoization of In-Progress Function Calls
      1. createUser Is Not Idempotent
      2. Challenges
    12. Streaming Promise Events
      1. Delivering More Information
      2. Delegating the Event Stream
      3. To Be Continued…
      4. Challenges
    13. Getting the First Result from a Set of Promises
      1. Which Promise Fired?
      2. A Fly in the Soup
      3. delegateEventStream Redux
      4. Challenges
    14. A Deferred Queue
      1. Challenges
    15. when2: An Improved jQuery.when
      1. Using when2 to Time Out a Single Promise
      2. Differences from $.when
      3. Challenges
    16. Timing Out Promises
      1. Challenges
    17. Controlling Your Own Destiny
      1. Challenges
    18. Deactivating a Promise
      1. Challenges
  5. 4. More Time in the Mental Gymnasium
    1. Do You Really Understand jQuery Deferreds?
    2. Promises/A+
    3. Promises Are First-Class Objects for Function Calls
    4. Asynchronous Data Structures
    5. Advantages of Deferreds
    6. Difficulties with Deferreds
    7. Further Reading
  6. A. Hints for Selected Challenges
    1. A Replacement for the setTimeout Function
    2. Messaging in Chrome Extensions
    3. Accessing Chrome Local Storage
    4. Running Promise-Returning Functions One by One
    5. A Promise Pool with an emptyPromise Method
    6. Displaying Google Maps
    7. Communicating with a Web Worker
    8. Using Web Sockets
    9. Automatically Retrying Failing Deferred Calls
    10. Memoization
    11. Short-Term Memoization of In-Progress Function Calls
    12. Streaming Promise Events
    13. Getting the First Result from a Set of Promises
    14. A Deferred Queue
    15. when2: An Improved jQuery.when
    16. Timing Out Promises
    17. Controlling Your Own Destiny
    18. Deactivating a Promise
  7. B. The Promises/A+ Specification
    1. Terminology
    2. Requirements
      1. Promise States
      2. The then Method
      3. The Promise Resolution Procedure
  8. C. Converting an ArrayBuffer to Base 64
  9. Colophon
  10. Copyright

Product information

  • Title: Learning jQuery Deferreds
  • Author(s): Terry Jones, Nicholas H. Tollervey
  • Release date: December 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449369354