Extracting Results from allSettled
You'll learn: How to process the Promise.allSettled() results array to cleanly separate successful values from failures.
Understanding the Result Structure
When Promise.allSettled() completes, you get an array of result objects—but these objects have different shapes depending on whether each promise fulfilled or rejected. Each object has a status property ("fulfilled" or "rejected"), plus either a value property (for success) or a reason property (for failure).
To actually use these results, you need to examine each object's status and extract the appropriate data.
Extracting by Filtering
The most straightforward approach is to filter the results into two groups:
const results = await Promise.allSettled([
Promise.resolve(10),
Promise.reject("Error 1"),
Promise.resolve(20),
Promise.reject("Error 2")
]);
const successes = results
.filter(result => result.status === "fulfilled")
.map(result => result.value);
const failures = results
.filter(result => result.status === "rejected")
.map(result => result.reason);
console.log(successes); // [10, 20]
console.log(failures); // ["Error 1", "Error 2"]
Processing in a Single Pass
For better performance, you can separate results in one loop:
const successes = [];
const failures = [];
for (const result of results) {
if (result.status === "fulfilled") {
successes.push(result.value);
} else {
failures.push(result.reason);
}
}
Think of it like sorting mail: You have a mixed pile of delivered letters and return-to-sender envelopes. You need to check each item's status and sort it into the correct bin.
Key Takeaway: After Promise.allSettled(), filter by status and extract either value (fulfilled) or reason (rejected) to work with the actual data from your promises.