拒绝处理不开火

编程入门 行业动态 更新时间:2024-10-22 21:42:50
本文介绍了拒绝处理不开火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

拒绝处理不开火

本文发布于:2023-11-26 00:03:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!