Chapter 13. Avoiding the callback hell with Deferred

 

This chapter covers

  • What promises are and why they’re important
  • The Deferred object
  • How to manage multiple asynchronous operations
  • Resolving and rejecting a promise

For a long time, JavaScript developers have used callback functions to perform several tasks such as running operations after a given amount of time (using set-Timeout()), or at regular intervals (using setInterval()), or to react to a given event (click, keypress, load, and so on). We’ve discussed and employed callbacks extensively to perform asynchronous operations; for example, in chapter 6 where we focused on events, in chapter 8 when we talked about animations, and in chapter 10 where we covered Ajax. Callback functions are simple and get the job done, but they become unmanageable as soon as you need to execute many asynchronous operations, either in parallel or in sequence. The situation where you have a lot of nested callbacks, or independent callbacks that have to be synchronized, is often referred to as the “callback hell.”

Today, websites and web applications are often powered by JavaScript code more than backend code only (this is the era of API-driven services, isn’t it?). For this reason, developers need a better way to manage and synchronize asynchronous operations to keep their code readable and maintainable.

13.1. Introduction to promises

 
 

13.2. The Deferred and Promise objects

 

13.3. The Deferred methods

 
 

13.4. Promisifying all the things

 
 

13.5. Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest