unhandledrejection

翻译不完整。 请帮助我们翻译这篇文章!

Promise 被 reject 且没有 reject 处理器的时候,会触发 unhandledrejection 事件;这可能发生在 window 下,但也可能发生在 Worker 中。 这对于调试回退错误处理非常有用。

是否冒泡 No
是否可取消 Yes
接口 PromiseRejectionEvent
事件处理器属性 onunhandledrejection

使用备注

unhandledrejection 继承自 PromiseRejectionEvent,而 PromiseRejectionEvent 又继承自 Event。因此unhandledrejection 含有 PromiseRejectionEventEvent 的属性和方法。

例子

Here we have a few examples showing ways you can make use of the unhandledrejection event. The event includes two useful pieces of information:

我们将通过几个例子来展示 unhandledrejection 事件的使用方式。该事件主要包含两部分有用的信息:

promise
The actual Promise which was rejected with no handler available to deal with the rejection.
特定的 Promise 被 reject 而没有被相应的异常处理方法所处理时
reason
The reason that would have been passed into the rejection handler if one had existed. See catch() for details.
将会传入异常处理方法中的错误原因(如果存在),查看 catch() 相关以获取更多细节。

基本的异常上报

此示例只是将有关未处理的 Promise rejection 信息打印到控制台。

window.addEventListener("unhandledrejection", event => {
  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
});

您还可以使用 onunhandledrejection 事件处理程序属性来设置事件侦听器:

window.onunhandledrejection = event => {
  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
};

防止默认处理

许多环境(例如 Node.js ) 默认情况下会向控制台打印未处理的 Promise rejections。您可以通过添加一个处理程序来防止 unhandledrejection 这种情况的发生,该处理程序除了您希望执行的任何其他任务之外,还可以调用 preventDefault() 来取消该事件,从而防止该事件冒泡并由运行时的日志代码处理。这种方法之所以有效,是因为 unhandledrejection 是可以取消的。

window.addEventListener('unhandledrejection', function (event) {
  // ...您的代码可以处理未处理的拒绝...

  // 防止默认处理(例如将错误输出到控制台)

  event.preventDefault();
});

说明

Specification Status Comment
HTML Living Standard
unhandledrejection
Living Standard Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
unhandledrejection eventChrome Full support 49Edge Full support ≤79Firefox Full support 69
Full support 69
Full support 68
Disabled
Disabled From version 68: this feature is behind the dom.promise_rejection_events.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 36Safari Full support 11WebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 68
Disabled
Full support 68
Disabled
Disabled From version 68: this feature is behind the dom.promise_rejection_events.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 36Safari iOS Full support 11.3Samsung Internet Android Full support 5.0

Legend

Full support  
Full support
No support  
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

参考

[1] The corresponding event handler property is defined on the WindowEventHandlers mixin, which is available on both the Window interface and all types of Worker interfaces.