Skip to curriculum
Jump to the editor

13.3. Promise.allSettled

Medium
Topics

This question is in

13. Promise from scratch
15–30 min

A settings page saves five independent preferences at once. If one save fails, the other four still count, and the page should tell the user exactly which one failed. Promise.all is the wrong tool here: it gives up at the first failure and hides the rest. Promise.allSettled waits for every promise, whatever happens, and reports each outcome.

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

Signature

JAVASCRIPT
function promiseAllSettled(iterable) // returns a Promise for an array of records
  • iterable: anything for...of can loop over; its items can be promises, thenables or plain values.
  • The result fulfills with one record per item, in the order of the input:
    • { status: "fulfilled", value } for an item that fulfilled,
    • { status: "rejected", reason } for an item that rejected.

Requirements

  1. promiseAllSettled returns a Promise, and it does not throw.
  2. It fulfills only when every item has settled, fulfilled or rejected; never early.
  3. Each record has exactly two properties: status and value, or status and reason. Nothing else.
  4. Records are in the order of the input, not the order the items settled in.
  5. A rejection never rejects the result: it becomes a record. That includes reasons like undefined, 0 or null.
  6. Plain values become fulfilled records; thenables are followed, exactly as Promise.resolve would.
  7. An empty iterable fulfills with []. Any iterable works (arrays, Set, strings, generators), and it is read during the call.
  8. The only way the result rejects: a value that is not iterable (TypeError), or an error thrown while iterating (that error).
  9. The result is a new array of new record objects.
  10. It does not call Promise.allSettled.

Examples

JAVASCRIPT
promiseAllSettled([Promise.resolve(1), Promise.reject(new Error("no")), "x"])
  .then(console.log);
// [
//   { status: "fulfilled", value: 1 },
//   { status: "rejected", reason: Error: no },
//   { status: "fulfilled", value: "x" },
// ]
JAVASCRIPT
promiseAllSettled([]).then(console.log); // []
JAVASCRIPT
promiseAllSettled([Promise.reject(undefined)]).then(console.log);
// [{ status: "rejected", reason: undefined }]
JAVASCRIPT
promiseAllSettled(null).catch((error) => console.log(error instanceof TypeError)); // true

Environment

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

Hints

  1. How can a then with two handlers turn both outcomes into the same kind of thing?
  2. When the last item settles first, how do you know the array is not complete yet?

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…