liveBook · Manning

Promise chain

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A sequence of linked promise operations in which the outcome of one promise feeds subsequent handlers.

A promise chain links asynchronous steps: each `.then()` passes its outcome onward, while a rejection can reach a later `.catch()`. This is useful when one operation depends on another. section 31.0

function nextImage() {
  return getRandomImage().then(fade).then(pause).then(nextImage);
}

The image workflow fetches an image, fades it in, pauses, then calls `nextImage` again through the chain. section 34.3

Calling `nextImage` again this way keeps extending the same chain. Instead, `repeat` starts a fresh chain each time and propagates rejection to its returned promise, avoiding unbounded chain growth; `start` can catch the rejection and restart. section 34.3

function start() {
  return repeat(nextImage).catch(start);
}

`start` uses `repeat(nextImage)` and catches failures by calling `start` again. section 34.3

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Works through a repeating image chain, its memory leak, and rejection propagation; stops short of broader promise internals.
Examples
Uses a random-image workflow that fetches, fades, pauses, and repeats, with a catch that restarts on failure.
What it assumes
Assumes familiarity with basic promises.
What you can do after
Understand how linked handlers carry outcomes and rejections, and how to prevent a repeating workflow from extending one chain indefinitely.
How it connects
Connects promises with asynchronous operations, error handling, and recursive function calls.

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 Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.