The Two-Argument .then() Form
You'll learn that .then() can accept two callbacks—one for success and one for rejection—though this is rarely the best approach.
The Second Argument
So far, you've seen .then() take one callback for the success case. But .then() actually accepts a second, optional callback that handles rejection:
promise.then(
(value) => {
// Success handler
console.log('Fulfilled:', value);
},
(error) => {
// Rejection handler
console.log('Rejected:', error);
}
);
The first function runs if the promise fulfills; the second runs if it rejects.
Why It Exists (But Why You Shouldn't Use It)
This two-argument form was part of the original Promise design. Technically, it works:
fetch('/api/data')
.then(
(response) => response.json(),
(error) => console.error('Fetch failed:', error)
);
However, there's a critical problem: the rejection handler only catches errors from the original promise, not from the success callback itself. If response.json() throws an error, the rejection handler won't catch it.
Why .catch() is Clearer
Using .catch() avoids this confusion:
fetch('/api/data')
.then((response) => response.json())
.catch((error) => console.error('Something failed:', error));
Now .catch() handles errors from both the fetch and the JSON parsing—exactly what you usually want.
When You Might See It
You'll encounter the two-argument form in older codebases or specific scenarios where you need distinct handling, but modern JavaScript style heavily favors .catch() for clarity and predictability.
Key Takeaway: .then() accepts both success and rejection callbacks, but using .catch() separately provides clearer, more reliable error handling across the entire promise chain.