Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT JAVASCRIPT
13.4. Promise.race
Easy
Topics
This question is in
13. Promise from scratchThe 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
function promiseRace(iterable) // returns a Promiseiterable: anythingfor...ofcan loop over; its items can be promises, thenables or plain values.- Returns a new
Promisethat fulfills or rejects like the first item to settle.
Requirements
promiseRacereturns a newPromise, and it does not throw.- The first item to settle wins: if it fulfills, the result fulfills with its value; if it rejects, the result rejects with its reason.
- Nothing that settles after the winner changes the result.
- A plain value counts as already fulfilled, so it beats any pending promise. Thenables are followed, exactly as
Promise.resolvewould; theirthenruns in a later microtask, never during the call. - When several items have already settled, the one listed first wins.
- An empty iterable gives a promise that stays pending forever.
- Any iterable works (arrays,
Set, strings, generators), and it is read during the call. - A value that is not iterable rejects with a
TypeError; an error thrown while iterating rejects with that error. - It does not call
Promise.race.
Examples
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"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"promiseRace([new Promise(() => {}), 7]).then(console.log); // 7
promiseRace([1, 2]).then(console.log); // 1: listed firstpromiseRace([]); // pending forever: no item can ever winEnvironment
- Plain JavaScript in a browser page; the tests call the global
promiseRace. - You may use
new PromiseandPromise.resolve. You may not callPromise.raceitself: one test replaces it for a moment to check. - Cancelling the losers is out of scope: promises cannot be cancelled.
Hints
- What happens when you call
resolveon a promise that is already settled? - Why might
Promise.resolve(item)be better than checking for athenmethod 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…