Web/API/PromiseRejectionEvent/PromiseRejectionEvent
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 errorDOMString
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
Specification | Status | Comment |
---|---|---|
HTML Living StandardThe definition of 'the PromiseRejectionEvent interface' in that specification. | Living Standard | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
PromiseRejectionEvent() constructor
|
Chrome
Full support 49 |
Edge
Full support ≤79 |
Firefox Full support 69 Full support 69 Full support 68 Disabled' From version 68: this feature is behind the |
IE
No support No |
Opera
Full support 36 |
Safari
Full support 11 |
WebView Android
Full support 49 |
Chrome Android
Full support 49 |
Firefox Android Full support 68 Full support 68 Disabled' From version 68: this feature is behind the |
Opera Android
Full support 36 |
Safari iOS
Full support 11.3 |
Samsung 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.
See also
PromiseRejectionEvent() by Mozilla Contributors is licensed under CC-BY-SA 2.5.