Recovering from Errors with Catch
You'll learn how to handle errors gracefully and allow your promise chain to continue instead of failing completely.
What Recovery Means
When a promise rejects, .catch() doesn't just log the error—it can return a value that lets the chain keep going. Think of it like a safety net: if someone falls (an error occurs), you can catch them, help them back up (provide a fallback value), and they continue walking (the chain proceeds successfully).
How Catch Enables Recovery
When .catch() returns a value, that value becomes a fulfilled promise in the chain. Subsequent .then() handlers receive this recovery value as if nothing went wrong:
fetchUserData()
.then(data => data.email)
.catch(error => {
console.log('Using default email');
return 'user@example.com'; // Recovery value
})
.then(email => {
console.log(`Email: ${email}`); // Receives either real or default email
return sendWelcome(email);
});
If fetchUserData() fails, the catch returns a default email, and sendWelcome() still runs.
When NOT to Recover
If you want the error to propagate, don't return a value—just let .catch() handle logging or cleanup:
.catch(error => {
logError(error);
// No return = error continues propagating
})
Or explicitly re-throw:
.catch(error => {
logError(error);
throw error; // Pass it along
})
Key Takeaway: Returning a value from .catch() transforms a rejected promise into a fulfilled one, allowing your chain to recover gracefully and continue with fallback data.