PromiseRejectionEvent
{{APIRef("HTML DOM")}}
{{AvailableInWorkers}}
The PromiseRejectionEvent
interface represents events which are sent to the global script context when JavaScript {{jsxref("Promise")}}
s are rejected. These events are particularly useful for telemetry and debugging purposes.
For details, see Promise rejection events.
{{InheritanceDiagram}}
Constructor
{{domxref("PromiseRejectionEvent.PromiseRejectionEvent", "PromiseRejectionEvent()")}}
- : Creates a
PromiseRejectionEvent
event, given the type of event (unhandledrejection
orrejectionhandled
) and other details.
- : Creates a
Instance properties
Also inherits properties from its parent {{domxref("Event")}}
.
{{domxref("PromiseRejectionEvent.promise")}}
{{ReadOnlyInline}}
- : The JavaScript
{{jsxref("Promise")}}
that was rejected.
- : The JavaScript
{{domxref("PromiseRejectionEvent.reason")}}
{{ReadOnlyInline}}
- : A value or
{{jsxref("Object")}}
indicating why the promise was rejected, as passed to{{jsxref("Promise.reject()")}}
.
- : A value or
Instance methods
This interface has no unique methods; inherits methods from its parent {{domxref("Event")}}
.
Events
{{domxref("Window/rejectionhandled_event", "rejectionhandled")}}
- : Fired when a JavaScript
{{jsxref("Promise")}}
is rejected, and after the rejection is handled by the promise’s rejection handling code.
- : Fired when a JavaScript
{{domxref("Window/unhandledrejection_event", "unhandledrejection")}}
- : Fired when a JavaScript
{{jsxref("Promise")}}
is rejected but there is no rejection handler to deal with the rejection.
- : Fired when a JavaScript
Examples
This simple example catches unhandled promise rejections and logs them for debugging purposes.
window.onunhandledrejection = (e) => {
console.log(e.reason);
};
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- Using promises
{{jsxref("Promise")}}
{{domxref("Window/rejectionhandled_event", "rejectionhandled")}}
{{domxref("Window/unhandledrejection_event", "unhandledrejection")}}