JavaScript Interview Question #87

What does Promise.allSettled() do?

Promises, Async/Await, Fetch & Modern JavaScript Mid-Level Intermediate

Quick Interview Answer

Promise.allSettled waits until every supplied Promise has settled, whether fulfilled or rejected. It returns an array of result objects containing a status and either a value or reason.

Detailed Explanation

Promise.allSettled waits until every supplied Promise has settled, whether fulfilled or rejected. It returns an array of result objects containing a status and either a value or reason.

Use it when operations are independent and the application needs the outcome of every operation instead of failing the entire group after the first rejection.

Code Example

const results = await Promise.allSettled([
    loadProfile(),
    loadRecommendations(),
    loadNotifications()
]);

for (const result of results) {
    if (result.status === "fulfilled") {
        console.log(result.value);
    } else {
        console.error(result.reason);
    }
}