Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled promise rejections doesn't emit unhandledRejection event in the browser #205

Closed
thomasguillory opened this issue Jun 3, 2016 · 4 comments

Comments

@thomasguillory
Copy link

Using babel-polyfill, it seems I'm unable to catch promise rejections with this syntax:

window.addEventListener('unhandledRejection', (event) => {
   ...
})

However I'm able to catch them with

window.onunhandledrejection = (event) => {
   ...
}

I think it's related to this line:

} else if(handler = global.onunhandledrejection){

@zloirock
Copy link
Owner

zloirock commented Jun 3, 2016

Yep, it still not implemented. I'll add it later. Feel free add a PR.

@alexbakerdev
Copy link

alexbakerdev commented Nov 13, 2017

Hey, was just going to have a go implementing this, but didn't realise there is no polyfill for event/custom event for the reasons described here #354.

So not really sure how to resolve this.

Also it seems like maybe a large refactor to implement, here is my naive attempt with comments on why it wouldn't work:

      if(isUnhandled(promise)){
        if(isNode){
          process.emit('unhandledRejection', value, promise);
        } else {
          /**
          This is required to call onunhandledrejection on any browser except chrome
          but using in chrome this will cause it to be called twice as it
          will also be triggered when the event is dispatched on the window.
          */
          if(handler = global.onunhandledrejection){
            handler({promise: promise, reason: value});            
          }

          // This doesn't work in IE11-
          var unhandledRejectionEvent = new Event('unhandledrejection');

          unhandledRejectionEvent.promise = promise;
          unhandledRejectionEvent.reason = value;

          global.dispatchEvent(unhandledRejectionEvent)
        }

      // Also what condition should be used to determine logging like:
      // console.error('Unhandled promise rejection', value);
      } record.a = undefined;

I don't really have much experience with older browser quirks, so some help would be appreciated.

@Jessidhia
Copy link

You should be able to use document.createEvent('Event') in this case, despite being deprecated.

@zloirock
Copy link
Owner

zloirock commented Dec 9, 2017

Added to v3 branch.

@zloirock zloirock closed this as completed Dec 9, 2017
@zloirock zloirock mentioned this issue Dec 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants