PromiseRejectionEvent.promise

PromiseRejectionEvent事件对象的 promise 属性是只读的,表明 Promise 被 reject 的原因。你可以通过检查PromiseRejectionEvent.reason来了解 Promise 为什么被 reject。

Syntax

promise = PromiseRejectionEvent.promise

Value

一个被 reject 的,并且错误未被处理的Promise

Examples

下面的例子监听了未被处理的 promise,如果reason 是一个对象,并且其code属性包含了一段文本“Module not read.”,一个空闲的回调函数被声明,当任务执行错误时会进行重试

event.preventDefault() 用来表明该 promise 已被处理

js
window.onunhandledrejection = function (event) {
  if (event.reason.code && event.reason.code == "Module not ready") {
    window.requestIdleCallback(function (deadline) {
      loadModule(event.reason.moduleName).then(performStartup);
    });
    event.preventDefault();
  }
};

Specifications

Specification
HTML
# dom-promiserejectionevent-promise

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
promise

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见