04.ii Promise.catch()
Метод promise.catch(onRejected) возвращает новый Promise реагирующий только на переход в rejected состояние. Если catch обработчик отработал, то Promise считается возвращенным в fulfilled состояние.
const promise = Promise.resolve("Success.");
promise
.then(value => {
console.log(value); // "Success!"
return Promise.reject("Failure.");
})
.catch(e => {
console.log(e); // Failure
})
.then(
() => {
console.log("after a catch the chain is restored");
},
() => {
console.log("Not fired due to the catch");
}
);
// Success.
// Failure.
// after a catch the chain is restoredErrors thrown inside asynchronous functions will act like uncaught errors:
Errors thrown after resolve is called will be silenced:
Catch errors on normal flow
Если есть Promise и на нем установлены две реакции на fulfilled и rejected случаи, то проблема в том, что исключение произошедшее в fulfilled случае не будет перехвачено в rejected обработчике:
Решение:
Handling exceptions in Promise-based functions
If exceptions are thrown inside the callbacks of then() and catch() then that’s not a problem, because these two methods convert them to rejections.
However, things are different if you start your async function by doing something synchronous:
If an exception is thrown in line A then the whole function throws an exception. There are two solutions to this problem.
returning a rejected Promise
executing the sync code inside a callback
Unhandled rejected Promises
Before a rejection is reported, an event is dispatched that you can listen to:
The event is an instance of PromiseRejectionEvent whose two most important properties are:
promise: the Promise that was rejectedreason: the value with which the Promise was rejected
The following example demonstrates how this event works:
Last updated
Was this helpful?