unhandledrejection

This translation is in progress.

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() 相关以获取更多细节。

基本的异常上报

This example simply logs information about the unhandled promise rejection to the console.

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

You can also use the onunhandledrejection event handler property to set up the event listener:

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

Preventing default handling

Many environments (such as Node.js) report unhandled promise rejections to the console by default. You can prevent that from happening by adding a handler for unhandledrejection events that—in addition to any other tasks you wish to perform—calls preventDefault() to cancel the event, preventing it from bubbling up to be handled by the runtime's logging code. This works because unhandledrejection is cancelable.

window.addEventListener('unhandledrejection', function (event) {
  // ...your code here to handle the unhandled rejection...

  // Prevent the default handling (such as outputting the
  // error to the console

  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 YesFirefox 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 No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox 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 No support NoSafari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
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.