Skip to curriculum
Jump to the editor

13.2. Promise.all

Medium
Topics

This question is in

13. Promise from scratch
15–30 min

A dashboard loads the user, their projects and their notifications at the same time, and shows the page once all three have arrived. Waiting for them one after another would add the three delays together; starting all three and waiting for the group takes only as long as the slowest. Promise.all is that "wait for the group": it turns many promises into one promise for an array of results, and it fails fast when any of them fails.

Write promiseAll(iterable) in promise-all.js, a function that behaves like Promise.all for every case below.

Signature

JAVASCRIPT
function promiseAll(iterable) // returns a Promise for an array
  • iterable: anything you can loop over with for...of: an array, a Set, a string, a generator. Its items can be promises, thenables (objects with a then method) or plain values.
  • Returns a Promise that fulfills with a new array of the values, or rejects with the first reason.

Requirements

  1. promiseAll returns a Promise, and it does not throw: every problem, even a bad argument, becomes a rejection.
  2. The array has the values in the order of the input, whatever order they settle in.
  3. It fulfills only when every item has fulfilled: never early, even when the last item settles first.
  4. A plain value counts as already fulfilled, and a thenable is followed, exactly as Promise.resolve would.
  5. An empty iterable fulfills with [].
  6. Any iterable works, not only arrays. The whole iterable is read during the call.
  7. It rejects as soon as one item rejects, with that item's reason: the first rejection in time wins, and nothing after it changes the result.
  8. A value that is not iterable (5, undefined, a plain object) rejects with a TypeError. An error thrown while iterating rejects with that error.
  9. Holes in an array count as undefined; the same promise listed twice gives its value twice.
  10. The result is a new array; the input is not changed.
  11. It does not call Promise.all.

Examples

JAVASCRIPT
const slow = new Promise((resolve) => setTimeout(resolve, 100, "slow"));
promiseAll([slow, 2, Promise.resolve(3)]).then(console.log);
// after 100 ms: ["slow", 2, 3]: input order, not finishing order
JAVASCRIPT
promiseAll([]).then(console.log);            // []
promiseAll(new Set(["a", "b"])).then(console.log); // ["a", "b"]
JAVASCRIPT
promiseAll([Promise.reject(new Error("no")), new Promise(() => {})])
  .catch((error) => console.log(error.message)); // "no", without waiting for the second
JAVASCRIPT
promiseAll(5).catch((error) => console.log(error instanceof TypeError)); // true

Environment

  • Plain JavaScript in a browser page; the tests call the global promiseAll.
  • You may use new Promise and Promise.resolve. You may not call Promise.all itself: one test replaces it for a moment to check.
  • this-based calls (promiseAll.call(SomeClass, items)) are out of scope.

Hints

  1. If item 2 fulfills first, how do you know where its value goes, and how do you know when the array is complete?
  2. What does a for...of loop do with a number, and where would that error go if the loop ran inside the executor of new Promise?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…