简体   繁体   中英

How to tell whether promise has error handler

Is it possible, within a new Promise((resolve, reject) => { //here }) block to tell whether a promise has a catch handler (or error handler, more generally, as for .then(..., errorHandler) clauses)?

I'd like to print an error message in case the promise hasn't, but otherwise don't.

You may not be able to check in the promise scope, but you could add a listener for a un-handled rejection in the browser. A event will be emitted if there was a un-hanlded promise.

Browser

window.addEventListener('unhandledrejection', function(event) {
    // event.promise error handling 
});

Node

process.on('unhandledRejection', function(err, promise) {
    // promise error handling 
});

https://developer.mozilla.org/en-US/docs/Web/API/Window/onunhandledrejection https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM