Home > Web Front-end > JS Tutorial > Catching unhandled Promise errors

Catching unhandled Promise errors

一个新手
Release: 2017-10-13 09:21:23
Original
2186 people have browsed it

By listening to the unhandledrejection event, you can capture unhandled Promise errors.

In order to ensure readability, this article adopts free translation rather than literal translation, and the source code has been extensively modified. In addition, the copyright of this article belongs to the original author, and the translation is for learning only.

When writing asynchronous code using Promise, use reject to handle errors. Sometimes, developers usually ignore this, resulting in some errors not being handled. For example:

function main() {
asyncFunc()
.then(···)
.then(() => console.log('Done!'));
}
Copy after login

Because the catch method is not used to capture the error, when asyncFunc()Functionreject, the error thrown is not handled.

This blog will introduce how to catch unhandled Promise errors in browsers and Node.js respectively.

Unhandled Promise errors in browsers

Some browsers (such as Chrome) are able to catch unhandled Promise errors.

unhandledrejection

Listen to the unhandledrejection event to capture the unhandled Promise error:

window.addEventListener(
'unhandledrejection', event => ···);
Copy after login


This event is an instance of PromiseRejectionEvent, which has the 2 most important properties:

  • promise: reject Promise

  • reason: reject value of Promise

Sample code:

window.addEventListener('unhandledrejection', event =>
{
console.log(event.reason); // 打印"Hello, Fundebug!"
});
 
function foo()
{
Promise.reject('Hello, Fundebug!');
}
 
foo();
Copy after login


##Fundebug's JavaScript error The monitoring plug-in listens to the

unhandledrejection event, so it can automatically capture unhandled Promise errors.

rejectionhandled

When a Promise error is not handled initially, but is later handled, the

rejectionhandled event will be triggered:

window.addEventListener(
'rejectionhandled', event => ···);
Copy after login
This event is an instance of

PromiseRejectionEvent.

Sample code:

##
window.addEventListener('unhandledrejection', event =>
{
console.log(event.reason); // 打印"Hello, Fundebug!"
});
 
window.addEventListener('rejectionhandled', event =>
{
console.log('rejection handled'); // 1秒后打印"rejection handled"
});
 
 
function foo()
{
return Promise.reject('Hello, Fundebug!');
}
 
var r = foo();
 
setTimeout(() =>
{
r.catch(e =>{});
}, 1000);
Copy after login
##Unhandled Promise errors in Node.js


Listen to the

unhandledRejection

event to capture unhandled Promise errors:

process.on(
'unhandledRejection', (reason, promise) => ···);
Copy after login
Sample code:

process.on('unhandledRejection', reason =>
{
console.log(reason); // 打印"Hello, Fundebug!"
});
 
function foo()
{
Promise.reject('Hello, Fundebug!');
}
 
foo();
Copy after login

The above is the detailed content of Catching unhandled Promise errors. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template