In other words, we usually cannot "try catch" an exception simply by calling another method.
But we can handle the exception of a promise by
promise
.then()
.catch();
or
promise
.then(fulfillmentHandler, rejectionHandler);
If the promise.then()
in the first case threw an exception, how can invoking catch()
, which is a method, on that promise handle the exception that happened previously?
We usually have to handle an exception by wrapping it using
try {
// doSomething
} catch (err) {
// handle the error
}
so if the exception happened for the promise that the then()
returned, doesn't the code have to handle the exception when promise.then()
has run? Simply invoking a method on an object usually would not be able to handle its exception (in this case invoking catch()
on the promise that then()
returned), because there is no such wrapping?