When You Need Everything at Once
Often you need to wait for multiple async operations to complete, not just one. The Promise API provides powerful methods for this.
1. Promise.all([p1, p2, p3])
Runs all promises in parallel and waits for all of them to succeed.
- ✅ Success: Returns an array of all results, in the same order as the input.
- ❌ If ANY ONE fails: Immediately rejects with that error (all other results are discarded).
const [user, products, settings] = await Promise.all([
fetch('/api/user').then(r => r.json()),
fetch('/api/products').then(r => r.json()),
fetch('/api/settings').then(r => r.json())
]);
2. Promise.allSettled([p1, p2, p3])
Waits for all promises to finish, regardless of whether they succeed or fail. Returns an array of result objects.
const results = await Promise.allSettled([p1, p2, p3]);
results.forEach(result => {
if (result.status === 'fulfilled') console.log(result.value);
if (result.status === 'rejected') console.error(result.reason);
});
3. Promise.race([p1, p2])
Returns the result of the first promise to settle (success or failure).
4. Promise.any([p1, p2])
Returns the result of the first promise to succeed. Only rejects if ALL promises fail.
Tip
Promise.allSettled is the safest choice when loading data from multiple independent sources. One broken server won't prevent you from displaying data from the others.