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

后端 未结 1 1388
陌清茗
陌清茗 2021-01-24 07:58

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 circumstan

相关标签:
1条回答
  • 2021-01-24 08:33

    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);
    
    0 讨论(0)
提交回复
热议问题