English | 2015 | ISBN: 978-1449373214 | 94 Pages | PDF, EPUB, MOBI | 11 MB
- 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
provide a host of APIs for asynchronous I/O.
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.
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.