0
0
Node.jsframework~10 mins

Promise.all for parallel execution in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run multiple promises in parallel using Promise.all.

Node.js
const promise1 = Promise.resolve(3);
const promise2 = Promise.resolve(5);

Promise.[1]([promise1, promise2])
  .then(results => {
    console.log(results); // [3, 5]
  });
Drag options to blanks, or click blank then click option'
Aall
Brace
Cany
Dresolve
Attempts:
3 left
💡 Hint
Common Mistakes
Using Promise.race instead of Promise.all
Using Promise.resolve which does not accept an array
2fill in blank
medium

Complete the code to handle the results of Promise.all with async/await syntax.

Node.js
async function fetchData() {
  const results = await Promise.all([1]);
  console.log(results);
}

fetchData();
Drag options to blanks, or click blank then click option'
AfetchUser() && fetchPosts()
BfetchUser(), fetchPosts()
C[fetchUser(), fetchPosts()]
D{fetchUser(), fetchPosts()}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing promises separated by commas without array brackets
Using curly braces instead of square brackets
3fill in blank
hard

Fix the error in the code to correctly catch errors from Promise.all.

Node.js
Promise.all([promiseA, promiseB])
  .then(results => {
    console.log(results);
  })
  .[1](error => {
    console.error('Error:', error);
  });
Drag options to blanks, or click blank then click option'
Afinally
Bcatch
Cthen
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using then instead of catch for error handling
Using finally which runs regardless of success or failure
4fill in blank
hard

Fill both blanks to create a function that runs two async tasks in parallel and returns their results.

Node.js
async function runTasks() {
  const results = await Promise.[1]([task1(), task2()]);
  return results.[2](result => result * 2);
}
Drag options to blanks, or click blank then click option'
Aall
Bmap
Cfilter
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter or reduce instead of map for transforming results
Using Promise.race instead of Promise.all
5fill in blank
hard

Fill all three blanks to create a function that fetches user data and posts in parallel, then logs user name and post count.

Node.js
async function fetchUserData() {
  const [[1], [2]] = await Promise.[3]([getUser(), getPosts()]);
  console.log(`User: ${user.name}, Posts: ${posts.length}`);
}
Drag options to blanks, or click blank then click option'
Auser
Bposts
Call
Drace
Attempts:
3 left
💡 Hint
Common Mistakes
Using Promise.race which resolves on first promise
Not destructuring the results array