Promises
Learn about promises, promises/A+, and thenables and how they work, and also the concept of promisification in Node.js.
We'll cover the following
Promises are part of the ECMAScript 2015 standard (or ES6, which is why they’re also called ES6 promises) and have been natively available in Node.js since version 4. But the history of promises goes back a few years earlier, when there were dozens of implementations around, initially with different features and behaviors. Eventually, the majority of those implementations settled on a standard called Promises/A+.
Promises represent a big step ahead toward providing a robust alternative to continuation-passing style callbacks for propagating an asynchronous result. As we’ll see, the use of promises will make all the major asynchronous control flow constructs easier to read, less verbose, and more robust compared to their callback-based alternatives.
What’s a Promise
?
A Promise
is an object that embodies the eventual result (or error) of an asynchronous operation. In promises jargon, we say that a Promise
is pending when the asynchronous operation is not yet complete, it’s fulfilled when the operation successfully completes and rejected when the operation terminates with an error. Once a Promise
is either fulfilled or rejected, it’s considered settled.
To receive the fulfillment value or the error (reason) associated with the rejection, we can use the then()
method of a Promise
instance. The following is its signature:
Get hands-on with 1400+ tech skills courses.