在promise中返回promise时可能未处理的错误 [英] Possibly unhandled Error when returning promise inside a promise

查看:55
本文介绍了在promise中返回promise时可能未处理的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

SuccessfulPromise().then(function() {
    return rejectedPromise();
}).catch(function(err) {
    console.log(err);
});

我是否有意收到上述错误消息,或者这是一个错误?该错误在catch块中得到处理,尽管这我得到了错误消息可能未处理的错误.

Is it intentional that i get the above error message or is it an error? The error gets handled in the catch block and although this i get the error message Possibly unhandled Error.

推荐答案

您说的是

问题出在 sinon.stub().returns(Promise.rejected('error')).在分配给错误处理程序之前,已经执行了Promise.

The problem was with sinon.stub().returns(Promise.rejected('error')). The promise got executed before assigned to an error handler.

我想指出的是,这是正确的.当承诺被拒绝时,Bluebird会检查链,以查看是否有任何错误处理程序将其处理,否则,它将触发 possfullyUnhandledRejection 事件.因此,如果拒绝是在分配处理程序之前发生的,则会收到错误报告".

I'd like to point out that this is correct. When a promise is rejected, Bluebird checks the chain to see if any error handlers will handle it, and if not it will trigger the possiblyUnhandledRejection event. So if the rejection happens before having assigned a handler, you get a "false report".

但是!有一个原因叫它可能:为其分配处理程序后,系统仍会检测到该错误的处理,然后它将触发 UnhandledRejectionHandled 事件已记录.您可以使用后一个事件来确保您不会因处理错误而被错误的未处理拒绝所困扰.

However! There is a reason it's called possibly: The system still detects handling of that error once you do assign a handler to it, and it will then trigger an UnhandledRejectionHandled event as documented. You can use the latter event to ensure that you are not bothered with false unhandled rejections past the point of handling them.

这篇关于在promise中返回promise时可能未处理的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆