Home >Web Front-end >JS Tutorial >Promise implements asynchronously
This time I will bring you Promise to implement asynchronous implementation. What are the precautions for Promise to implement asynchronous implementation? The following is a practical case, let's take a look.
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!')); }Since the error is not captured using the catch method, the error thrown is not handled when the asyncFunc() function rejects. This blog will introduce how to catch unhandled Promise errors in browsers and
Node.js.
Unhandled Promise error in browser
Some browsers (such as Chrome) are able to catch unhandled Promise errors.unhandledrejection
Listen to the unhandledrejectionevent to capture unhandled Promise errors:
window.addEventListener('unhandledrejection', event => ···);This event is a PromiseRejectionEvent instance, which has the 2 most important
properties:
promise: reject Promise reason: Promise’s reject value Sample code:window.addEventListener('unhandledrejection', event => { console.log(event.reason); // 打印"Hello, Fundebug!" }); function foo() { Promise.reject('Hello, Fundebug!'); } foo();Fundebug's rejectionhandled When a Promise error is not handled initially, but is handled later, the rejectionhandled event will be triggered: window.addEventListener('rejectionhandled', event => ···); 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);
Unhandled Promise error in Node.js
Listen to the unhandledRejection event to capture unhandled Promise errors: process.on('unhandledRejection', (reason, promise) => ···); Sample code:process.on('unhandledRejection', reason => { console.log(reason); // 打印"Hello, Fundebug!" }); function foo() { Promise.reject('Hello, Fundebug!'); } foo();
Note: Node.js v6.6.0 will report unhandled Promise errors by default, so it is okay not to listen to the unhandledrejection event.
Fundebug's Node.js error monitoring plug-in listens to the unhandledRejection event, so it can automatically catch unhandled Promise errors. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:The above is the detailed content of Promise implements asynchronously. For more information, please follow other related articles on the PHP Chinese website!