当Promise被rejected且有rejection处理器时会在全局触发
rejectionhandled
事件(通常是发生在window下,但是也可能发生在Worker中)。应用于调试一般应用回退。当Promise被rejected且没有rejection处理器处理时会触发unhandledrejection事件。这两个事件协同工作。
是否冒泡 | No |
---|---|
是否可取消 | No |
接口 | PromiseRejectionEvent |
事件处理器属性 | onrejectionhandled |
例子
你可以使用rejectionhandled事件在控制台打印出被rejected的Promise,以及被rejected的原因:
window.addEventListener("rejectionhandled", event => {
console.log("Promise rejected; reason: " + event.reason);
}, false);
说明
Specification | Status | Comment |
---|---|---|
HTML Living Standard rejectionhandled |
Living Standard | Initial definition. |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.