liveBook · Manning

Promise.all

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

A Promise class method that returns a promise fulfilled when all input promises fulfill, or rejected when any input promise rejects.

Promise.all([...]) returns a new promise that waits for every input promise to fulfill. If any input promise rejects, the returned promise rejects. Use it when several results are needed together. section 30.4 section 30.3

function getAuthorAndArticles(authorId) {
  const authorReq = load(`/authors/${authorId}/details`)
  const articlesReq = load(`/authors/${authorId}/articles`)

  return Promise.all([authorReq, articlesReq])                   1
}

getAuthorAndArticles(37).then( ([author, articles]) => {         2
  // author and articles have loaded
});
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":65},{\"line\":4,\"ch\":66}],[{\"line\":7,\"ch\":65},{\"line\":7,\"ch\":66}]]"}
!@%STYLE%@!

The author details and articles load through separate requests. Promise.all joins them; the .then callback receives both results for use together. section 30.3

Credit-check page

To render a credit page, combine Promise.all for the credit-availability request with Promise.race for the TransUnion and Equifax score requests. The page needs availability and at least one score. section 30.4

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

How deep it goes
Defines the aggregate promise and demonstrates joining requests, without detailing implementation internals.
Examples
Uses author-and-article requests and a credit-check task combining Promise.all with Promise.race.
What it assumes
Explains promises as eventual values and .then callbacks before summarizing Promise.all.
What you can do after
Apply Promise.all to wait for multiple requests before using their combined results.
How it connects
Links Promise.all to promises, .then, Promise.resolve, and Promise.race.

Also explained in

Compare how 2 books explain Promise.all →

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.