liveBook · Manning

Promise chain

As explained in The Joy of JavaScript

A promise chain is a sequence of linked Promise reactions in which each step receives the result or failure of the previous step.

A promise chain is a sequence of linked Promise reactions. Each then or catch returns a new promise, passing its result or failure to the next step. This creates a unidirectional flow of data transformations. section 8.2.3

Link each transformation

Promise.resolve('aabbcc')\n .then(unique)\n .then(join)\n .then(toUpper);\n\nEvery handler receives the preceding step's result. Calling handlers separately on the same promise creates disjoint operations rather than a chain.

Return nested promises when an inner asynchronous flow must rejoin the outer chain; omitting return lets its result or error escape. A catch can recover with a default value, while finally performs completion work.

How The Joy of JavaScript covers it

How deep it goes
Explains linking, settlement, nested-promise flattening, broken chains, error recovery, and finally, with runnable snippets.
Examples
Uses string transformations, nested promises, thrown errors, default recovery values, unhandled rejection warnings, and finally.
What you can do after
Builds linear Promise workflows, preserves nested results and failures, and diagnoses accidental disjoint chains.
How it connects
Connects Promise chains to composition, algebraic data types, asynchronous control flow, and error handling.

Also explained in

Compare how 2 books explain promise chain →

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.