Error Propagation in Promises
You'll learn how rejected promises automatically travel down a chain of .then() calls until they encounter a .catch() handler.
The Core Idea
When a promise rejects (or an error is thrown inside a .then() callback), JavaScript doesn't stop executing your entire program. Instead, the rejection skips all subsequent .then() handlers and jumps directly to the first .catch() it finds. This is similar to how try/catch works in synchronous code—errors bubble up until something catches them.
Think of it like a waterslide with multiple platforms: if you slip (reject) at the top, you don't stop at each platform on the way down—you slide straight to the safety net (.catch()) at the bottom.
How It Works
Promise.reject('Something went wrong')
.then(() => {
console.log('This will be skipped');
})
.then(() => {
console.log('This will also be skipped');
})
.catch(error => {
console.log('Caught:', error); // Caught: Something went wrong
});
The rejection bypasses both .then() handlers and goes directly to .catch().
If an error is thrown inside a .then(), the same propagation happens:
Promise.resolve(5)
.then(value => {
throw new Error('Oops');
})
.then(() => {
console.log('Skipped');
})
.catch(error => {
console.log(error.message); // Oops
});
Without a .catch(), the rejection remains unhandled—a critical mistake that can crash your application or create silent failures.
Key Takeaway: Rejected promises automatically propagate through .then() chains until a .catch() handler stops them, making centralized error handling straightforward and predictable.