promise-worker-bi icon indicating copy to clipboard operation
promise-worker-bi copied to clipboard

Add an event listener for unhandledrejections?

Open ejdaly opened this issue 5 years ago • 1 comments

Hi,

Thanks for the library.

I can see there is an event listener to catch global "error" events, but not on global "unhandledrejection" events.

E.g if my worker code does something like below, it won't get handled by the registerError callback.

const test = function() {
  return new Promise(function(resolve, reject) {
    setTimeout(() => {
      reject(new Error("test error"))
      }, 5000);
    })
}

test();

Would it make sense to add a global listener for that, e.g. could just "re-throw" that error

self.addEventListener("unhandledrejection", function (e) {
  e.preventDefault();
  throw e.reason;
});

ejdaly avatar Jan 08 '20 22:01 ejdaly

Yeah, it probably does make sense to handle "unhandledrejection" events similar to "error" events. I'm not sure when I'll get around to adding it, but probably eventually. Or, PR welcome :)

I wonder if there are any other events that should be handled similarly too..

dumbmatter avatar Jan 10 '20 15:01 dumbmatter