How to Handle an Array of Promises in JavaScript
When working with asynchronous code in JavaScript, you often need to run multiple promises at the same time.
For example:
- Fetching data from multiple API endpoints
- Uploading several files simultaneously
- Running multiple database queries
- Processing multiple asynchronous tasks in parallel
Handling these operations one by one can make your application slower and harder to manage.
Instead, JavaScript provides powerful built-in methods to efficiently manage arrays of promises, allowing multiple asynchronous operations to run in parallel and be handled in a clean, structured way.
In this blog, weβll explore how to work with multiple promises step by step.
Before diving deeper, make sure you understand the basics of Promises.
πΉ The Problem: Multiple Async Tasks
Imagine you need to fetch data from 3 APIs:
await fetchUser();
await fetchPosts();
await fetchComments();
If you wait for each one sequentially, it becomes slow.
We need them to run in parallel.
πΉ Creating an Array of Promises
Example:
const promise1 = Promise.resolve("User");
const promise2 = Promise.resolve("Posts");
const promise3 = Promise.resolve("Comments");
const promises = [promise1, promise2, promise3];
Now we have an array of promises.
But how do we handle them together?
1οΈβ£ Promise.all()
This is the most common method.
Waits for ALL promises to succeed.
If ONE fails β everything fails.
Promise.all(promises)
.then(results => {
console.log(results);
})
.catch(error => {
console.error(error);
});
Output: User, Posts, CommentsWhat If One Promise Fails?
const promise2 = Promise.reject("Failed!");
Then Promise.all() immediately fails.
Thatβs sometimes dangerous.
2οΈβ£ Promise.allSettled()
This method waits for ALL promises, whether they succeed or fail.
It never rejects.
Promise.allSettled(promises)
.then(results => console.log(results));
It returns:
[
{ status: "fulfilled", value: "User" },
{ status: "rejected", reason: "Error" }
]
3οΈβ£ Promise.race()
Returns the FIRST promise that settles (success OR failure).
Promise.race(promises)
.then(result => console.log(result));
Useful for:
- Timeouts
- Fastest response
4οΈβ£ Promise.any()
Returns the first SUCCESSFUL promise.
If all fail β throws error.
πΉ Parallel vs Sequential Execution
β Sequential (Slow)
await fetchUser();
await fetchPosts();
await fetchComments();
Each waits for the previous.
β Parallel (Fast)
await Promise.all([
fetchUser(),
fetchPosts(),
fetchComments()
]);
All run at the same time.
Much faster.
πΉ Real-World Example
Imagine loading a dashboard:
- User data
- Notifications
- Messages
- Settings
Instead of:
Wait β Wait β Wait β WaitYou do:
Load everything together
Thatβs why Promise.all() is powerful.
πΉ Common Mistakes
β Forgetting to return promises inside map
β Using sequential await unnecessarily
β Not handling rejected promises
β Not understanding that Promise.all fails fast
πΉ Performance Tip
When using arrays:
const results = await Promise.all(
urls.map(url => fetch(url))
);
This runs all fetches concurrently.
Professional pattern.
πΉ Quick Comparison Table
| Method | Fails Fast? | Waits All? | Returns First? |
|---|---|---|---|
| Promise.all | β Yes | β No | β No |
| Promise.allSettled | β No | β Yes | β No |
| Promise.race | Depends | β No | β Yes |
| Promise.any | β Only if all fail | β No | β First success |
πΉ Conclusion
When working with multiple async tasks:
- Use
Promise.all()for parallel success - Use
Promise.allSettled()when you need all results - Use
Promise.race()for fastest result - Use
Promise.any()for first success