JS-Dev-101 practice question 14 of 25

Refer to the code: 01 let car1 = new Promise((_, reject) => 02 setTimeout(reject, 2000, "Car 1 crashed in")); 03 let car2 = new Promise(resolve => 04…

Full set: 148 questions
Refer to the code: 01 let car1 = new Promise((_, reject) => 02 setTimeout(reject, 2000, "Car 1 crashed in")); 03 let car2 = new Promise(resolve => 04 setTimeout(resolve, 1500, "Car 2 completed")); 05 let car3 = new Promise(resolve => 06 setTimeout(resolve, 3000, "Car 3 completed")); 07 08 Promise.race([car1, car2, car3]) 09 .then(value => { 10 let result = '$(value) the race.'; 11 }) 12 .catch(err => { 13 console.log("Race is cancelled.", err); 14 }); What is the value of result when Promise.race executes?
Answer options
A: Car 2 completed the race. Correct answer
B: Car 3 completed the race.
C: Race is cancelled.
D: Car 1 crashed in the race.
Correct Answer:
A. Car 2 completed the race.
Explanation

Promise.race() returns a promise that settles (fulfills or rejects) as soon as one of the input promises settles. In this scenario, car2 resolves in 1500ms, which is faster than car1 rejecting (2000ms) and car3 resolving (3000ms). Therefore, the Promise.race fulfills with the value of car2 ("Car 2 completed"). The .then() block is executed, and assuming the single quotes in the result assignment are a typo for backticks (template literal), the value of result becomes "Car 2 completed the race.".

Why Incorrect

B. car3 is the slowest promise to resolve (3000ms), so it cannot win the race.
C. The .catch() block is not executed because the race is won by a resolving promise (car2), not a rejecting one.
D. car1 rejects at 2000ms, which is slower than car2 resolving at 1500ms. The race is already over before car1 crashes.

References

1. MDN Web Docs, Promise.race(). "The Promise.race() static method takes an iterable of promises as input and returns a single Promise. This returned promise settles with the eventual state of the first promise that settles." (First paragraph).

2. ECMAScript® 2023 Language Specification, Section 27.2.4.6 Promise.race ( iterable ).

Scroll to Top