简体   繁体   English

如何判断Promise是否具有错误处理程序

[英]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)? 是否有可能在new Promise((resolve, reject) => { //here })块中判断一个new Promise((resolve, reject) => { //here })是否具有catch处理程序(或更.then(..., errorHandler)错误处理程序,如.then(..., errorHandler)条款)?

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. 您可能无法签入Promise范围,但是可以在浏览器中添加用于未处理拒绝的侦听器。 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 https://developer.mozilla.org/zh-CN/docs/Web/API/Window/onunhandledrejection https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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