Complete the code to run multiple promises in parallel using Promise.all.
const promise1 = Promise.resolve(3); const promise2 = Promise.resolve(5); Promise.[1]([promise1, promise2]) .then(results => { console.log(results); // [3, 5] });
Promise.all runs all promises in parallel and waits for all to resolve.
Complete the code to handle the results of Promise.all with async/await syntax.
async function fetchData() {
const results = await Promise.all([1]);
console.log(results);
}
fetchData();Promise.all expects an array of promises as input.
Fix the error in the code to correctly catch errors from Promise.all.
Promise.all([promiseA, promiseB])
.then(results => {
console.log(results);
})
.[1](error => {
console.error('Error:', error);
});The catch method handles errors from promises.
Fill both blanks to create a function that runs two async tasks in parallel and returns their results.
async function runTasks() {
const results = await Promise.[1]([task1(), task2()]);
return results.[2](result => result * 2);
}Promise.all runs tasks in parallel. Then map doubles each result.
Fill all three blanks to create a function that fetches user data and posts in parallel, then logs user name and post count.
async function fetchUserData() {
const [[1], [2]] = await Promise.[3]([getUser(), getPosts()]);
console.log(`User: ${user.name}, Posts: ${posts.length}`);
}Destructure results as user and posts. Use Promise.all to run both fetches in parallel.