Skip to curriculum
Jump to the editor

13.5. Promise.any with AggregateError

Medium
Topics

This question is in

13. Promise from scratch
15–30 min

An app can read a user's profile from three places: a memory cache, the browser's storage, and the server. Any one of them is good enough, and a failure in one should not matter while another can still answer. Only when all of them fail is there an error to show, and then the user (or the log) needs every reason, not just one. Promise.any does exactly that: the first success wins, and if there is none, it rejects with an AggregateError that carries every failure.

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

Signature

JAVASCRIPT
function promiseAny(iterable) // returns a Promise
  • iterable: anything for...of can loop over; its items can be promises, thenables or plain values.
  • Returns a Promise that fulfills with the first value, or rejects with an AggregateError. An AggregateError is a built-in error class: new AggregateError(errors, message) makes one whose errors property is an array of the reasons.

Requirements

  1. promiseAny returns a Promise, and it does not throw.
  2. It fulfills with the value of the first item to fulfill. Rejections before that are ignored.
  3. While at least one item is pending, a rejection never rejects the result.
  4. When every item has rejected, it rejects with an AggregateError. Its errors is an array with every reason, in the order of the input, not the order they came in. Reasons like undefined or 0 are kept.
  5. An empty iterable rejects with an AggregateError whose errors is [].
  6. Nothing that settles after the winner changes the result.
  7. A plain value counts as already fulfilled; thenables are followed, exactly as Promise.resolve would.
  8. Any iterable works (arrays, Set, strings, generators), and it is read during the call.
  9. A value that is not iterable rejects with a TypeError (not an AggregateError); an error thrown while iterating rejects with that error.
  10. It does not call Promise.any.

Examples

JAVASCRIPT
promiseAny([Promise.reject(new Error("cache miss")), Promise.resolve("from server")])
  .then(console.log); // "from server"
JAVASCRIPT
const late = new Promise((_, reject) => setTimeout(reject, 100, "late"));
promiseAny([late, Promise.reject("early")]).catch((error) => {
  console.log(error instanceof AggregateError); // true
  console.log(error.errors);                    // ["late", "early"]: input order
});
JAVASCRIPT
promiseAny([]).catch((error) => console.log(error.errors)); // []
JAVASCRIPT
promiseAny(42).catch((error) => console.log(error instanceof TypeError)); // true

Environment

  • Plain JavaScript in a browser page; the tests call the global promiseAny.
  • You may use new Promise, Promise.resolve, the AggregateError class and the other combinators. You may not call Promise.any itself: one test replaces it for a moment to check.
  • The exact text of the error's message is up to you.

Hints

  1. Promise.all fails at the first rejection and succeeds when all fulfill. Which of those two does any flip?
  2. When the last item in the input rejects first, where should its reason go?

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…