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

Errors thrown by callback swallowed #2

Closed
zbjornson opened this issue Sep 8, 2019 · 3 comments
Closed

Errors thrown by callback swallowed #2

zbjornson opened this issue Sep 8, 2019 · 3 comments

Comments

@zbjornson
Copy link

Hey there,

Errors thrown by the queued callback will get swallowed, there should be a catch on the promise. @domenic corrected this in the polyfill I'd posted on MDN (which was removed for some reason; I just restored it). https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/queueMicrotask#Polyfill

@feross
Copy link
Owner

feross commented Sep 10, 2019

@zbjornson Thanks for the issue. This is the code I found on MDN:

if (typeof window.queueMicrotask !== "function") {
  window.queueMicrotask = function (callback) {
    Promise.resolve()
      .then(callback)
      .catch(e => setTimeout(() => { throw e; }));
  };
}

Can you explain why the exception is thrown in a setTimeout? Is that so that it can't be caught by another .catch at a higher level?

@zbjornson
Copy link
Author

I think it's this difference, from the OP of whatwg/html#512:

Promise.resolve().then(f) ... creates a promise object and stores any thrown errors there, instead of letting them propagate to "report an exception"

https://html.spec.whatwg.org/multipage/webappapis.html#report-the-error

With setTimeout, the error will invoke window.onerror (like queueMicrotask). Without setTimeout, it won't.

> window.onerror = e => console.log("onerror:", e);

> queueMicrotask(() => { throw new Error("eek"); })
"onerror: Script error."
❌Uncaught Error: eek

> Promise.resolve().then(() => { throw new Error("oops"); }).catch(e => setTimeout(() => { throw e; }))
"onerror: Script error."
❌Uncaught Error: oops

> Promise.resolve().then(() => { throw new Error("oops"); }).catch(e => { throw e; })
// no onerror handler
❌Uncaught (in promise) Error: oops

@feross
Copy link
Owner

feross commented Sep 10, 2019

Thanks for the explanation. I released a fix for this as 1.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants