Promise.all and Error Short-Circuiting
You'll learn how Promise.all bails out immediately when any single promise fails, which can leave other promises running but unhandled.
The Short-Circuit Behavior
When you use Promise.all([p1, p2, p3]), you're waiting for all promises to succeed. But here's the catch: the moment any one promise rejects, Promise.all immediately rejects with that error. It doesn't wait for the others to finish.
This is called short-circuiting—like a circuit breaker tripping at the first sign of trouble.
What Happens to the Other Promises?
Here's the tricky part: the other promises don't stop running. They continue executing in the background, but Promise.all has already moved on to the .catch() block. If those other promises later reject, those errors go unhandled because you've already left the .all() context.
Think of it like starting three tasks simultaneously, but the moment one fails, you walk away from the project. The other two tasks keep running, but nobody's watching them anymore.
Why This Matters
Promise.all([
fetchUserData(), // takes 2 seconds
fetchOrders(), // fails at 1 second
fetchPreferences() // takes 3 seconds
])
.then(results => console.log('All done'))
.catch(err => console.error('Failed:', err));
// Catches fetchOrders error at 1 second
// fetchUserData and fetchPreferences keep running!
If fetchPreferences() later rejects, that error might silently disappear unless you've attached individual error handlers to each promise beforehand.
When to Care
This matters when your promises have side effects (writing to a database, charging a credit card, sending emails). A short-circuit might leave you with partially completed operations and hidden failures.
Key Takeaway: Promise.all rejects immediately on the first failure and stops watching the remaining promises, potentially leaving their errors unhandled—plan your error strategy accordingly.