capypad
0 day streak
javascript / intermediate
Snippet

Resilient Parallel Async with Promise.allSettled

Unlike Promise.all, Promise.allSettled waits for all promises to finish regardless of whether they fulfilled or rejected.

snippet.js
javascript
1
2
3
const promises = [fetch('/api/1'), fetch('/api/2')];
const results = await Promise.allSettled(promises);
results.forEach(res => console.log(res.status));
Breakdown
1
Promise.allSettled(promises)
Executes multiple promises in parallel without short-circuiting on error.
2
res.status
Each result object contains a status string: 'fulfilled' or 'rejected'.