TypeScript · Middle · Question 64 of 75
How do you narrow the results of Promise.allSettled?
const results = await Promise.allSettled(tasks);
const values = results.filter((r) => r.status === 'fulfilled').map((r) => r.value);- ✓Correct answer: Check the
statusdiscriminant; since TS 5.5 the filter callback is inferred as a type predicate - 2Use
r.value!everywhere - 3Results are always fulfilled
Explanation
PromiseSettledResult<T> is a discriminated union of fulfilled (value) and rejected (reason) results.