0
0
Node.jsframework~20 mins

Promise.race and Promise.allSettled in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does Promise.race output?
Consider the following code snippet using Promise.race. What will be logged to the console?
Node.js
const p1 = new Promise(resolve => setTimeout(() => resolve('First'), 300));
const p2 = new Promise(resolve => setTimeout(() => resolve('Second'), 100));
Promise.race([p1, p2]).then(result => console.log(result));
APromise pending
B"First"
Cundefined
D"Second"
Attempts:
2 left
💡 Hint
Think about which promise resolves first based on the timeout durations.
state_output
intermediate
2:00remaining
What is the output of Promise.allSettled?
Given the following promises, what will Promise.allSettled return?
Node.js
const p1 = Promise.resolve('Success');
const p2 = Promise.reject('Error');
Promise.allSettled([p1, p2]).then(results => console.log(results));
APromise rejected with 'Error'
B[{status: 'fulfilled', value: 'Success'}, {status: 'fulfilled', value: 'Error'}]
C[{status: 'fulfilled', value: 'Success'}, {status: 'rejected', reason: 'Error'}]
D[{status: 'rejected', reason: 'Success'}, {status: 'rejected', reason: 'Error'}]
Attempts:
2 left
💡 Hint
Remember that allSettled waits for all promises and reports their status.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error with Promise.race?
Identify which code snippet will cause a syntax error when using Promise.race.
APromise.race(Promise.resolve(1), Promise.resolve(2)).then(console.log);
BPromise.race([Promise.resolve(1), Promise.reject(2)]).then(console.log);
CPromise.race([Promise.resolve(1), Promise.resolve(2)]).then(console.log);
DPromise.race([]).then(console.log);
Attempts:
2 left
💡 Hint
Check the expected argument type for Promise.race.
🔧 Debug
advanced
2:00remaining
Why does this Promise.allSettled code not log results?
This code does not log anything. What is the reason?
Node.js
const p1 = Promise.resolve('Done');
const p2 = Promise.reject('Fail');
Promise.allSettled([p1, p2]);
AMissing .then() to handle the results
BPromise.allSettled does not work with rejected promises
CPromises must be awaited with async/await
DThe array passed is empty
Attempts:
2 left
💡 Hint
Promises are lazy until you handle their result.
🧠 Conceptual
expert
2:00remaining
Which statement about Promise.race and Promise.allSettled is true?
Select the only true statement about Promise.race and Promise.allSettled:
APromise.allSettled resolves as soon as the first promise settles
BPromise.race resolves or rejects as soon as any promise settles
CPromise.race waits for all promises to settle before resolving
DPromise.allSettled rejects if any promise rejects
Attempts:
2 left
💡 Hint
Think about how each method handles multiple promises and their timing.