taylorhakes/promise-polyfill

unhandledrejection event not fired

achimmihca opened this issue · 2 comments

If not providing a catch method for a Promise, then a default error handler will be used.
Typically, this default error handler prints a message to the console.

However, one can add an EventListener to be notified of such missing error handling (see here):

window.addEventListener("unhandledrejection", event => {
  console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
});

Unfortunately, the Promise polyfill does not seem to trigger the corresponding event. I see a message on the console, but what is missing is something similar to

window.dispatchEvent(new PromiseRejectionEvent('unhandledrejection', { reason: theErrorObject, promise: thePromiseObject }));

where PromiseRejectionEvent is a subclass of CustomEvent.

I need such a hook to be notified about all uncaught errors in a Promise. I use this for logging etc.

The dispatchEvent should go to Promise._unhandledRejectionFn.
As workaround one could overwrite Promise._unhandledRejectionFn to fire the event (or to directly do the stuff that you want).

Closing issue because PR has become stale.