8Promises

In this chapter you'll learn about ES2015's promises, which simplify and standardize the handling of one-off asynchronous operations, reducing “callback hell” (and paving the way for ES2018's async functions, covered in Chapter 9). A promise is an object representing an asynchronous result (like an HTTP request completing or a one-off timer firing). You use a promise to observe the result of an asynchronous process, and/or give the promise to other code so that other code can observe that result. Promises are JavaScript's version of a pattern variously called promises, futures, or deferreds. They draw heavily on prior art, in particular the Promises/A+ specification1 and the work leading up to it.

Before we get started: You may have heard that promises are obsolete, because ES2018 added async functions (Chapter 9). That's mostly incorrect. It's true that you'll interact with promises differently when using async functions, but you'll still be interacting with them, and you'll still need to have a solid idea how they work. Also, there are times you'll use promises directly, even in an async function.

WHY PROMISES?

A promise doesn't do anything of its own, it's just a way to observe ...

Get JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.