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 questionsPromise.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.".
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.
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 ).
Keep practicing
Take the free JS-Dev-101 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JS-Dev-101 practice questions
- Question 1A developer executes: document.cookie; document.cookie = 'key=John Smith'; What is the be…
- Question 2A developer is required to write a function that calculates the sum of elements in an arr…
- Question 3Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, r…
- Question 401 function Animal(size, type) { 02 this.type = type || 'Animal'; 03 this.canTalk = false…
- Question 5Refer to the code snippet: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i …
- Question 6Code: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) …
