Skip to curriculum
Jump to the editor

13.4. Promise.race

Easy
Topics

This question is in

13. Promise from scratch
10–20 min

The same image is on two CDNs, and the page uses whichever answers first. A request should give up after five seconds. Both are races: several promises start together, and the first one to finish decides the outcome. Promise.race gives you a promise that settles exactly like the first of its inputs to settle, whether that one fulfilled or rejected.

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

Signature

JAVASCRIPT
function promiseRace(iterable) // returns a Promise
  • iterable: anything for...of can loop over; its items can be promises, thenables or plain values.
  • Returns a new Promise that fulfills or rejects like the first item to settle.

Requirements

  1. promiseRace returns a new Promise, and it does not throw.
  2. The first item to settle wins: if it fulfills, the result fulfills with its value; if it rejects, the result rejects with its reason.
  3. Nothing that settles after the winner changes the result.
  4. A plain value counts as already fulfilled, so it beats any pending promise. Thenables are followed, exactly as Promise.resolve would; their then runs in a later microtask, never during the call.
  5. When several items have already settled, the one listed first wins.
  6. An empty iterable gives a promise that stays pending forever.
  7. Any iterable works (arrays, Set, strings, generators), and it is read during the call.
  8. A value that is not iterable rejects with a TypeError; an error thrown while iterating rejects with that error.
  9. It does not call Promise.race.

Examples

JAVASCRIPT
const fast = new Promise((resolve) => setTimeout(resolve, 50, "fast"));
const slow = new Promise((resolve) => setTimeout(resolve, 500, "slow"));
promiseRace([slow, fast]).then(console.log); // after 50 ms: "fast"
JAVASCRIPT
const timeout = new Promise((_, reject) => setTimeout(reject, 100, new Error("timeout")));
promiseRace([new Promise(() => {}), timeout])
  .catch((error) => console.log(error.message)); // after 100 ms: "timeout"
JAVASCRIPT
promiseRace([new Promise(() => {}), 7]).then(console.log); // 7
promiseRace([1, 2]).then(console.log);                     // 1: listed first
JAVASCRIPT
promiseRace([]); // pending forever: no item can ever win

Environment

  • Plain JavaScript in a browser page; the tests call the global promiseRace.
  • You may use new Promise and Promise.resolve. You may not call Promise.race itself: one test replaces it for a moment to check.
  • Cancelling the losers is out of scope: promises cannot be cancelled.

Hints

  1. What happens when you call resolve on a promise that is already settled?
  2. Why might Promise.resolve(item) be better than checking for a then method yourself?

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…