JavaScript with Promises

JavaScript with Promises

English | 2015 | ISBN: 978-1449373214 | 94 Pages | PDF, EPUB, MOBI | 11 MB

Asynchronous JavaScript is everywhere, whether you’re using Ajax, AngularJS, Node.js, or WebRTC. This practical guide shows intermediate to advanced JavaScript developers how Promises can help you manage asynchronous code effectively—including the inevitable flood of callbacks as your codebase grows. You’ll learn the inner workings of Promises and ways to avoid difficulties and missteps when using them.
The ability to asynchronously fetch data and load scripts in the browser broadens the capabilities of JavaScript applications. But if you don’t understand how the async part works, you’ll wind up with unpredictable code that’s difficult to maintain. This book is ideal whether you’re new to Promises or want to expand your knowledge of this technology.

  • Understand how async JavaScript works by delving into callbacks, the event loop, and threading
  • Learn how Promises organize callbacks into discrete steps that are easier to read and maintain
  • Examine scenarios you’ll encounter and techniques you can use when writing real-world applications
  • Use features in the Bluebird library and jQuery to work with Promises
  • Learn how the Promise API handles asynchronous errors
  • Explore ECMAScript 6 language features that simplify Promise-related code

Asynchronous JavaScript

The number of asynchronous JavaScript APIs is rapidly growing. Web applications asynchronously fetch data and load scripts in the browser. Node.js and its derivatives
provide a host of APIs for asynchronous I/O.

Let’s start with a code snippet that frequently surprises people. The code makes an HTTP request using the XMLHttpRequest (XHR) object and uses a while loop that runs for three seconds. Although it is generally bad practice to implement a delay with the while loop, it’s a good way to illustrate how JavaScript runs. Read the code in Example 1-1 and decide whether the listener callback for the XHR object will ever be triggered.

Introducing Promises

This chapter introduced all the basic concepts of Promises. Keep these three points in mind:

  • A promise is a placeholder for a value that is usually the result of an asynchronous operation.
  • A promise has three states: pending, fulfilled, and rejected.
  • After a promise is fulfilled or rejected, its state and value can never be changed.

At this point you have walked through a number of examples that demonstrate the basic ways a promise is used and you are ready to run sequential asynchronous steps in your own code using promise chains. You should also be comfortable using APIs that return promises for asynchronous work.

One example of promises in the wild is in the CSS Font Load Events spec, which provides a FontFaceSet.load() function that returns a promise for loading fonts into the browser. Consider how you could use this function to only display text once a desired font is loaded in the browser.

Promises can be combined to orchestrate async tasks and structure code in various ways. Although a sequential workflow was provided here, you’ll soon want to use
promises in more advanced ways. The next chapter walks through a variety of ways you can use promises in your applications.

Working with Standard Promises

This chapter covered a number of scenarios that are likely to arise when using promises. It showed how one async function affects all the functions that come before it in the call stack. It also showed how to process an arbitrary number of tasks sequentially or in parallel. And how to build processing pipelines by chaining promises together.
All the topics in this chapter were addressed using the standard Promise API. This discussion is continued in Chapter 4 using expanded APIs that some promise libraries and frameworks provided.

Combining ECMAScript 6 Features with Promises

This chapter showed how some of the new language features in ES6 can be used with promises. These features all allow you to write less code to accomplish the same outcome. We began with simplifying access to fulfillment values using destructuring, followed by concise callback declarations using arrow functions. And we concluded
with how iterators and generators can be used to treat async functions that return promises as synchronous code.

This chapter also concludes the book. We started with the fundamentals of asynchronous programming in JavaScript and worked through the core concepts in Promises
and how to utilize them in a wide variety of scenarios. At this point you should be prepared to confidently manage async tasks with Promises, absorb new promisebased APIs such as Service Workers or Streams, and even create your own promisebased API.