我正在尝试使用 rejectionhandled 但我无法启动它.下面的代码应该基于我能找到的所有文档工作.我为 rejectionhandled 注册了一个监听器,然后拒绝一个承诺并捕获它.在启用了测试"标志的 chrome 和 firefox 中,会记录但未拒绝".我错过了什么?
window.addEventListener("rejectionhandled", e => console.log("rejected"), false);new Promise((resolve, reject) => setTimeout(reject, 1000)).catch(err => console.error("testing")) 解决方案rejectionhandled 事件 不会在每次处理的拒绝时触发,它只会在最初未处理的拒绝时触发(导致 unhandledrejection 事件),但随后确实附加了一个处理程序.>
事件是从 的 HTML 实现中触发的实现定义的 HostPromiseRejectionTracker 在 ECMAScript 规范中,其注释说
HostPromiseRejectionTracker 在两种情况下被调用:
- 当一个 promise 在没有任何处理程序的情况下被拒绝时,它会被调用,并将其操作参数设置为reject".
- 当处理程序第一次被添加到被拒绝的 promise 时,它会被调用,并将其操作参数设置为handle".
I'm trying to use rejectionhandled but I can't get it to fire. The code below should work based on all the documnation I could find. I register a listener for rejectionhandled then reject a promise and catch it. In both chrome and firefox with the flag enabled 'testing' is logged but not 'rejected'. What am I missing?
window.addEventListener("rejectionhandled", e => console.log("rejected"), false); new Promise((resolve, reject) => setTimeout(reject, 1000)).catch(err => console.error("testing"))解决方案
The rejectionhandled event doesn't fire on every handled rejection, it only fires on rejections that were not handled at first (causing unhandledrejection events) but then did subsequently get a handler attached.
The events are fired from the HTML implementation of the implementation-defined HostPromiseRejectionTracker in the ECMAScript spec, whose note says
HostPromiseRejectionTracker is called in two scenarios:
- When a promise is rejected without any handlers, it is called with its operation argument set to "reject".
- When a handler is added to a rejected promise for the first time, it is called with its operation argument set to "handle".
更多推荐
拒绝处理不开火
发布评论