Both are used to handle multiple Promises, but they behave very differently when some Promises fail.
πΉ Promise.all()
β What it does
- Waits for all Promises to resolve
- Fails fast if any Promise rejects
Promise.all([
Promise.resolve("A"),
Promise.reject("Error"),
Promise.resolve("B")
])
.then(console.log)
.catch(console.error);
β Output
Error
π§ Explanation
- As soon as one Promise rejects,
Promise.allrejects - Remaining Promises are ignored
π When to use Promise.all()
β All operations are required
β Failure of one means failure of whole task
β Example: loading critical data
πΉ Promise.allSettled()
β What it does
- Waits for all Promises to complete
- Never rejects
- Returns result of each Promise
Promise.allSettled([
Promise.resolve("A"),
Promise.reject("Error"),
Promise.resolve("B")
])
.then(console.log);
β Output
[
{ status: "fulfilled", value: "A" },
{ status: "rejected", reason: "Error" },
{ status: "fulfilled", value: "B" }
]
π§ Explanation
- Every Promise is tracked
- Useful for partial success handling
π₯ Key Differences (Quick Table)
| Feature | Promise.all | Promise.allSettled |
|---|---|---|
| Waits for all | β No | β Yes |
| Rejects on failure | β Yes | β No |
| Returns results | Only if all succeed | Always |
| Use case | All-or-nothing | Partial success |
π― Short Interview Answer
Promise.all()resolves only if all Promises resolve and rejects immediately on the first failure, whilePromise.allSettled()waits for all Promises to finish and returns their individual outcomes regardless of success or failure.
β One-line summary
Use Promise.all for all-or-nothing, Promise.allSettled when every result matters.