liveBook · Manning

Promise

As explained in The Joy of JavaScript

A JavaScript Promise is an object representing the eventual completion or failure of an asynchronous operation and its resulting value or reason.

A JavaScript Promise represents one eventual value. It gives asynchronous work a consistent interface whether data is nearby or remote, replacing nested callbacks with chains that read from one step to the next. A promise ultimately fulfills with a value or rejects with an error. section 8.2 section 8.2.1 section 8.2.2

Create and consume a promise

const result = new Promise((resolve, reject) => { const value = doWork(); if (value === null) reject(new Error('No value')); else resolve(value); }); result.then(useValue).catch(handleError); The constructor receives an executor, which starts when the promise is created—not when a handler is attached. The promise begins pending; resolving or rejecting settles it. section 8.2 section 8.2.2 section 8.6

Each then step returns another Promise, so sequential operations can stay flat. A step may return a plain value or another Promise; then flattens the latter automatically. Fulfillment carries the result forward, while catch handles rejection. This resembles mapping and chaining operations on algebraic data types, without requiring callers to manage each callback’s timing. section 1.5 section 8.2.2

A file-processing chain

A file workflow can check access, read the file with fs.promises, decode and tokenize its contents, count the resulting entries, then catch an error to report a missing file or insufficient permissions. Each operation has a distinct place in the chain rather than another level of callback nesting. section 8.2.4 section 8.2

Promises fit single asynchronous results. async/await expresses the same underlying behavior as a sequence of statements, while async generators address sequences delivered over time. Promises also have limits: their executors are not lazy, and there is no standard way to cancel a running promise. section 1.5 section 8.6

Figure 1.10 How Promise objects chain to form new Promise objects. Along the way, the same pattern repeats. Both success and rejection cases lead to a new Promise object returned.

Follow how each link in a chain creates a new Promise, including paths for fulfillment and rejection. section 1.5

How The Joy of JavaScript covers it

How deep it goes
Works through construction, settlement, chaining, and nested-Promise flattening, while leaving detailed event-loop scheduling aside.
Examples
Uses Promise construction, chained string transformations, and a filesystem workflow that checks access, reads, counts, and catches errors.
What it assumes
Assumes familiarity with callbacks and builds on algebraic data types while explaining Promise creation and states.
What you can do after
Apply it via the JavaScript Promise API to compose asynchronous operations and handle their failures in a flat chain.
How it connects
Connects callbacks, the event loop, algebraic data types, data locality, and async/await.

Also explained in

Compare how 7 books explain promise →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.