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 recipient of unhandledrejection
sends a rejectionhandled
event if it handles the rejection, so the system knows to stop treating the rejection as unhandled.
PromiseRejectionEvent = PromiseRejectionEvent(type, options);
The PromiseRejectionEvent()
constructor also inherits parameters from Event()
.
type
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
Object
specifying details about the rejection which occurred: promise
Promise
that was rejected.reason
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.A new PromiseRejectionEvent
configured as specified by the parameters.
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.
var myRejectionEvent = new PromiseRejectionEvent('unhandledrejection', { promise : myPromise, reason : 'My house is on fire' });
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'the PromiseRejectionEvent interface' in that specification. | Living Standard | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 49 | ? | No
|
No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | ? | No | ? | No
|
No | No | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent/PromiseRejectionEvent