PromiseRejectionEvent() - Web APIs 编辑

The PromiseRejectionEvent() constructor returns a newly created PromiseRejectionEvent, which represents events fired when a JavaScript Promise is rejected. With promise rejection events, it becomes possible to detect and report promises which fail and whose failures go unnoticed. It also becomes easier to write a global handler for errors.

There are two types of PromiseRejectionEvent: unhandledrejection is sent by the JavaScript runtime when a promise is rejected but the rejection goes unhandled. A rejectionhandled event is emitted if a promise is rejected but the rejection is caught by a rejection handler..

Syntax

promiseRejectionEvent = PromiseRejectionEvent(type, options);

Parameters

The PromiseRejectionEvent() constructor also inherits parameters from Event().

type
A string representing the name of the type of the PromiseRejectionEvent. This is case-sensitive and should be one of "rejectionhandled" or "unhandledrejection", to match the event names of the possible (non-synthetic) PromiseRejectionEvent events that user agents can actually fire).
options
An Object specifying details about the rejection which occurred:
promise
The Promise that was rejected.
reason
Any value or Object which represents the reason the promise was rejected. This can be anything from a numeric error code to an error DOMString to an object which contains detailed information describing the situation resulting in the promise being rejected.

Return value

A new PromiseRejectionEvent configured as specified by the parameters.

Examples

This example creates a new unhandledrejection event for the promise myPromise with the reason being the string "My house is on fire". The reason could just as easily be a number, or even an object with detailed information including the home address, how serious the fire is, and the phone number of an emergency contact who should be notified.

let myRejectionEvent = new PromiseRejectionEvent("unhandledrejection", {
  promise : myPromise,
  reason : "My house is on fire"
});

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'the PromiseRejectionEvent interface' in that specification.
Living StandardInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:47 次

字数:5383

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文