javascript / intermediate
Snippet
Robust Async with Promise.allSettled
Promise.allSettled waits for all promises to complete, regardless of whether they were fulfilled or rejected. This is ideal for batch operations where you want to know the outcome of every task instead of short-circuiting on the first error.
snippet.js
1
2
3
4
5
6
7
8
9
10
const tasks = [Promise.resolve('Done'),Promise.reject('Failed')];Promise.allSettled(tasks).then(results => {results.forEach(res => {if (res.status === 'fulfilled') console.log(res.value);else console.error(res.reason);});});
Breakdown
1
Promise.allSettled(tasks)
Waits for every promise in the array to reach a final state.
2
res.status === 'fulfilled'
Each result object contains a status property to distinguish success from failure.
3
res.reason
Contains the error message or reason for rejected promises.