JavaScript
Public
Process Mixed Results with Promise.allSettled
Run independent asynchronous tasks and keep both successful and failed outcomes.
#javascript
#promise
#async
#error-handling
JavaScript
async function loadDashboardResources() {
const requests = [
fetch('/api/profile').then((response) => response.json()),
fetch('/api/projects').then((response) => response.json()),
fetch('/api/notifications').then((response) => response.json()),
];
const results = await Promise.allSettled(requests);
return results.map((result, index) => {
if (result.status === 'fulfilled') {
return {
index,
ok: true,
data: result.value,
};
}
return {
index,
ok: false,
error: result.reason instanceof Error
? result.reason.message
: String(result.reason),
};
});
}
loadDashboardResources().then(console.table);