gpt4 book ai didi

javascript - 拒绝处理不开火

转载 作者:行者123 更新时间:2023-11-30 13:56:11 24 4
gpt4 key购买 nike

我正在尝试使用 rejectionhandled 但我无法启动它。下面的代码应该基于我能找到的所有文档工作。我为 rejectionhandled 注册了一个监听器,然后拒绝一个 promise 并捕获它。在启用标志的 chrome 和 firefox 中,“测试”会被记录但不会被“拒绝”。我错过了什么?

window.addEventListener("rejectionhandled", e => console.log("rejected"), false);
new Promise((resolve, reject) => setTimeout(reject, 1000)).catch(err => console.error("testing"))

最佳答案

rejectionhandled event不会在每次处理的拒绝时触发,它只会在最初未处理的拒绝时触发(导致 unhandledrejection events )但后来确实附加了处理程序。

事件是从 implementation-defined HostPromiseRejectionTracker 的 HTML 实现中触发的在 ECMAScript 规范中,其说明是

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".

关于javascript - 拒绝处理不开火,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57261713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com