Under what circumstances might bluebird's “Possibly unhandled Error” warning be wrong?

。_饼干妹妹 提交于 2019-12-04 05:18:42

问题


The word "possibly" suggests there are some circumstances where you can get this warning in the console even if you catch the error yourself.

What are those circumstances?


回答1:


This is pretty well explained in the docs:

Unhandled rejections/exceptions don't really have a good agreed-on asynchronous correspondence. The problem is that it is impossible to predict the future and know if a rejected promise will eventually be handled.

The [approach that bluebird takes to solve this problem], is to call a registered handler if a rejection is unhandled by the start of a second turn. The default handler is to write the stack trace to stderr or console.error in browsers. This is close to what happens with synchronous code - your code doesn't work as expected and you open console and see a stack trace. Nice.

Of course this is not perfect, if your code for some reason needs to swoop in and attach error handler to some promise after the promise has been hanging around a while then you will see annoying messages.

So, for example, this might warn of an unhandled error even though it will get handled pretty well:

var prom = Promise.reject("error");
setTimeout(function() {
    prom.catch(function(err) {
        console.log(err, "got handled");
    });
}, 500);


来源:https://stackoverflow.com/questions/24520401/under-what-circumstances-might-bluebirds-possibly-unhandled-error-warning-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!