Promise Rejection Events

Observe and construct Promise rejection events.

PromiseRejectionEvent extends Event and adds read-only promise and reason attributes.

const promise = Promise.reject("boom");
promise.catch(() => {});

const event = new PromiseRejectionEvent("unhandledrejection", {
  promise,
  reason: "boom",
});

event.reason; // "boom"
event.isTrusted; // false

The init dictionary must include promise; omitting it throws TypeError. The constructor must be called with new.

Global events

Elide dispatches unhandledrejection when a Promise is rejected without a handler. If a handler is attached later, it dispatches rejectionhandled.

globalThis.addEventListener("unhandledrejection", (event) => {
  console.error("Unhandled Promise:", event.reason);
});

globalThis.addEventListener("rejectionhandled", () => {
  console.log("The rejection now has a handler");
});

The onunhandledrejection and onrejectionhandled properties provide the corresponding single-listener form. Attach rejection handlers directly when the program can recover; use the global events for observation and reporting.