Ava
Ava

Reputation: 2132

rejectionhandled not firing

I'm trying to use rejectionhandled but I can't get it to fire. The code below should work based on all the documnation I could find. I register a listener for rejectionhandled then reject a promise and catch it. In both chrome and firefox with the flag enabled 'testing' is logged but not 'rejected'. What am I missing?

window.addEventListener("rejectionhandled", e => console.log("rejected"), false);
new Promise((resolve, reject) => setTimeout(reject, 1000)).catch(err => console.error("testing"))

Upvotes: 3

Views: 539

Answers (1)

Bergi
Bergi

Reputation: 664484

The rejectionhandled event doesn't fire on every handled rejection, it only fires on rejections that were not handled at first (causing unhandledrejection events) but then did subsequently get a handler attached.

The events are fired from the HTML implementation of the implementation-defined HostPromiseRejectionTracker in the ECMAScript spec, whose note says

HostPromiseRejectionTracker is called in two scenarios:

  • When a promise is rejected without any handlers, it is called with its operation argument set to "reject".
  • When a handler is added to a rejected promise for the first time, it is called with its operation argument set to "handle".

Upvotes: 6

Related Questions