0
0
Node.jsframework~20 mins

Promise.all for parallel execution in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise.all Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Promise.all example?
Consider the following Node.js code using Promise.all. What will be logged to the console?
Node.js
const p1 = Promise.resolve(10);
const p2 = new Promise(resolve => setTimeout(() => resolve(20), 100));
const p3 = Promise.resolve(30);

Promise.all([p1, p2, p3]).then(results => console.log(results));
A[10, 30, 20]
B[30, 20, 10]
CError: Promise rejected
D[10, 20, 30]
Attempts:
2 left
💡 Hint
Promise.all waits for all promises to resolve and keeps the order of the input array.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses Promise.all to fetch data?
You want to fetch data from three URLs in parallel using Promise.all. Which code snippet is correct?
Aconst results = Promise.all(fetch(url1), fetch(url2), fetch(url3));
Bconst results = await Promise.all([fetch(url1), fetch(url2), fetch(url3)]);
Cconst results = await Promise.all(fetch(url1), fetch(url2), fetch(url3));
Dconst results = Promise.all([fetch(url1), fetch(url2), fetch(url3)]).then();
Attempts:
2 left
💡 Hint
Promise.all expects a single array of promises.
🔧 Debug
advanced
2:00remaining
Why does this Promise.all code never resolve?
Look at this code snippet. Why does the Promise.all never resolve or reject?
Node.js
const p1 = new Promise(() => {});
const p2 = Promise.resolve(2);

Promise.all([p1, p2]).then(console.log).catch(console.error);
Ap1 never resolves or rejects, so Promise.all waits forever.
Bp2 rejects immediately causing Promise.all to hang.
CThe .then callback is missing, so no output occurs.
DPromise.all requires at least three promises to resolve.
Attempts:
2 left
💡 Hint
A promise that never settles blocks Promise.all from finishing.
state_output
advanced
2:00remaining
What is the value of 'results' after this Promise.all call?
Given the code below, what will be the value of 'results' inside the then callback?
Node.js
const p1 = Promise.resolve('a');
const p2 = Promise.resolve('b');
const p3 = Promise.resolve('c');

Promise.all([p1, p2, p3]).then(results => {
  // What is results?
  console.log(results);
});
A['a', 'b', 'c']
B['c', 'b', 'a']
C['a', 'c', 'b']
Dundefined
Attempts:
2 left
💡 Hint
Promise.all preserves the order of promises in the input array.
🧠 Conceptual
expert
2:00remaining
Which option best describes Promise.all behavior when one promise rejects?
What happens when one promise in Promise.all rejects while others are still pending?
APromise.all waits for all promises to settle before rejecting.
BPromise.all resolves with all fulfilled values and ignores rejections.
CPromise.all immediately rejects with that error, ignoring other promises.
DPromise.all retries the rejected promise until it resolves.
Attempts:
2 left
💡 Hint
Promise.all rejects as soon as any promise rejects.