What exactly is the difference between these two statements?
funcThatReturnsAPromise()
.then(() => { /* success */ })
.catch(() => { /* fail */ });
funcThatReturnsAPromise()
.then(() => { /* success */ }, () => { /* fail */ });
What exactly is the difference between these two statements?
funcThatReturnsAPromise()
.then(() => { /* success */ })
.catch(() => { /* fail */ });
funcThatReturnsAPromise()
.then(() => { /* success */ }, () => { /* fail */ });
Besides .catch(fn)
being a shortcut for .then(null, fn)
, the difference in your examples is that
funcThatReturnsAPromise()
.then(() => { /* success */ })
.catch(() => { /* fail */ });
// is equivalent to
const p1 = funcThatReturnsAPromise()
const p2 = p1.then(() => { /* success */ })
const p3 = p2.catch(() => { /*
executed if p1 is rejected
executed if p2 is rejected
*/ })
While the second one is
funcThatReturnsAPromise()
.then(() => { /* success */ }, () => { /* fail */ });
// equivalent to
const p1 = funcThatReturnsAPromise()
const p2 = p1.then(
() => { /* success */ },
() => { /*
executed if p1 is rejected
(p2 will be actually resolved by the result of this function only when p1 is rejected)
*/ }
);
.catch(foo)
is equal to .then(undefined, foo)
But there is a difference between your code samples:
funcThatReturnsAPromise()
.then(() => { /* success case of funcThatReturnsAPromise */ })
.catch(() => { /* both fail case of funcThatReturnsAPromise
and fail case of "then" function */ });
funcThatReturnsAPromise()
.then(() => { /* success case of funcThatReturnsAPromise */ },
() => { /* fail case of funcThatReturnsAPromise */ });
then(..) takes one or two parameters, the first for the fulfillment callback, and the second for the rejection callback. If either is omitted or is otherwise passed as a non-function value, a default callback is substituted respectively. The default fulfillment callback simply passes the message along, while the default rejection callback simply rethrows (propagates) the error reason it receives. catch(..) takes only the rejection callback as a parameter, and automatically substitutes the default fulfillment callback, as just discussed. In other words, it’s equivalent to then(null,..) :
p . then ( fulfilled );
p . then ( fulfilled , rejected );
p . catch ( rejected ); // or `p.then( null, rejected )`
then(..) and catch(..) also create and return a new promise, which can be used to express Promise chain flow control. If the fulfillment or rejection callbacks have an exception thrown, the returned promise is rejected. If either callback returns an immediate, non-Promise, non-thenable value, that value is set as the fulfillment for the returned promise. If the fulfillment handler specifically returns a promise or thenable value, that value is unwrapped and becomes the resolution of the returned promise.
—from "You Don't Know JS, Kyle Simpson