拒绝处理不开火 [英] rejectionhandled not firing

查看:40
本文介绍了拒绝处理不开火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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".

这篇关于拒绝处理不开火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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