JavaScript > Asynchronous JavaScript > Promises > Promise.all()
Using Promise.all() to Handle Multiple Promises
This example demonstrates how to use Promise.all()
in JavaScript to handle multiple promises concurrently and process their results once all promises have resolved.
Basic Promise.all() Example
This code snippet defines three asynchronous functions, getUserId
, getUserName
, and getUserEmail
, each simulating a task that takes some time to complete using setTimeout
and resolves with a value. Promise.all()
is then used to execute these promises concurrently. The then()
block receives an array of results in the same order as the promises passed to Promise.all()
. The catch()
block handles any errors that might occur in any of the promises.
// Define three asynchronous functions that return promises
function getUserId() {
return new Promise(resolve => {
setTimeout(() => {
resolve(123);
}, 500);
});
}
function getUserName(id) {
return new Promise(resolve => {
setTimeout(() => {
resolve(`User_${id}`);
}, 700);
});
}
function getUserEmail(name) {
return new Promise(resolve => {
setTimeout(() => {
resolve(`${name}@example.com`);
}, 1000);
});
}
// Use Promise.all() to execute the promises concurrently
Promise.all([getUserId(), getUserName(123), getUserEmail('User_123')])
.then(results => {
const [userId, userName, userEmail] = results;
console.log('User ID:', userId);
console.log('User Name:', userName);
console.log('User Email:', userEmail);
})
.catch(error => {
console.error('An error occurred:', error);
});
Concepts Behind the Snippet
Promise.all()
takes an iterable (typically an array) of promises as input. It returns a single Promise
that resolves when all of the promises in the iterable have resolved. The resolved value is an array containing the resolved values of each promise, in the same order as they appear in the input iterable. If any of the promises reject, Promise.all()
immediately rejects with the rejection reason of the first promise that rejected.
Real-Life Use Case
Consider a scenario where you need to fetch data from multiple APIs to render a user profile page. Each API call (e.g., fetching user details, user posts, user friends) can be represented as a promise. Using Promise.all()
, you can initiate all these API calls concurrently and wait for all the data to be fetched before rendering the profile page. This improves performance compared to fetching data sequentially.
Best Practices
catch()
block to handle potential rejections from any of the promises passed to Promise.all()
.Promise.all()
is beneficial when the order of promise resolution doesn't matter and you want to execute asynchronous operations concurrently.
Interview Tip
Understanding how Promise.all()
handles rejections is crucial. Be prepared to explain that if any of the promises reject, the entire Promise.all()
operation rejects with the reason of the first promise to reject. Also, know when Promise.all()
would be a better approach than sequentially resolving Promises.
When to use them
Use Promise.all()
when you have multiple asynchronous operations that can be executed in parallel, and you need to wait for all of them to complete before proceeding. This is suitable for scenarios where the order of completion doesn't matter, and you need to process the results of all operations together.
Memory Footprint
Promise.all()
keeps all the promises in memory until all are resolved or one rejects. If dealing with a very large number of promises, or promises that hold a significant amount of data, this could lead to increased memory usage. Consider alternatives like Promise.allSettled()
(if you need to know the outcome of each promise regardless of rejection) or processing promises in smaller batches if memory becomes a concern.
Alternatives
Promise.allSettled()
: Unlike Promise.all()
, Promise.allSettled()
waits for all promises to be either fulfilled or rejected. It returns an array of objects describing the outcome of each promise.Promise.race()
: Returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.
Pros
catch()
block for all promises.
Cons
Promise.all()
operation rejects, potentially masking other successful operations.Promise.all()
is only from the first promise to reject. Other potential errors from other promises are not reported.
FAQ
-
What happens if one of the promises passed to
Promise.all()
rejects?
If any of the promises reject,Promise.all()
immediately rejects with the rejection reason of the first promise that rejected. The other promises, even if they're still pending, will not affect the outcome. -
Does the order of promises in the input array matter?
Yes, the order of the promises in the input array matters. The resulting array of resolved values will be in the same order as the promises in the input array, regardless of the order in which they resolve. -
What is the difference between
Promise.all()
andPromise.allSettled()
?
Promise.all()
rejects if any of the promises reject, whilePromise.allSettled()
waits for all promises to either fulfill or reject and returns an array of objects indicating the outcome of each promise.