Promise Chaining
Promise Chaining in Modern JavaScript
One of the greatest advantages of Promises over callbacks is Promise Chaining. Because .then() always returns a brand-new Promise, you can chain sequential asynchronous operations into a clean, flat, readable pipeline with centralized error handling.
1. How Chaining Works
Whenever you return a value inside a .then() handler, the next .then() receives that value wrapped in a resolved Promise:
2. Returning Promises Inside .then()
If you return a new Promise from inside a .then() callback, the chain will pause until that inner Promise settles:
3. Centralized Error Handling
In a Promise chain, errors trickle down until they find the nearest .catch() handler. A single .catch() at the end can handle errors originating from any preceding step:
4. Recovering from Errors in a Chain
If you return a fallback value from inside .catch(), subsequent .then() handlers can continue execution:
5. Parallel Promise Combinators
When operations are independent and do not rely on each other, run them concurrently:
| Method | Behavior | Resolves When... | Rejects When... |
|---|---|---|---|
Promise.all() | Fail-fast parallel batch | All fulfill | Any single promise rejects |
Promise.allSettled() | Complete audit | All settle (fulfilled or rejected) | Never rejects |
Promise.race() | First responder | First promise settles | First promise settles |
Promise.any() | First success | First promise fulfills | All promises reject (AggregateError) |
Practice Quiz
Intro to Async/Await
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Promises Basics | Intro to Async/Await |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.