Quantcast
Channel: Pale Moon forum
Viewing all articles
Browse latest Browse all 2686

Browser Development • Unexpected "uncaught exception" using async functions

$
0
0
I was having a hard time understanding why I was getting "uncaught exception" errors in the console, although the promise reject was actually caught and my code was running as expected.

Upon investigation, I discovered the error was not happening on other browsers, and it seems to be a bug in Pale Moon.

CODE:

document.body.addEventListener('click', async function () {    async function usingThrow() {        throw Error('Using throw');    }    await usingThrow().catch(err => console.log('Caught: ' + err));});document.body.addEventListener('click', async function () {    async function usingPromiseReject() {        return Promise.reject('Using Promise.reject');    }    await usingPromiseReject().catch(err => console.log('Caught: ' + err));});
Results:
Caught: Using Promise.reject
Caught: Error: Using throw
uncaught exception: Using Promise.reject
Note this code also causes an error:

CODE:

document.body.addEventListener('click', async function () {    async function usingPromiseReject() {        return Promise.reject('Using Promise.reject');    }    await usingPromiseReject().catch(err => console.log('Caught: ' + err));    async function usingThrow() {        throw Error('Using throw');    }    await usingThrow().catch(err => console.log('Caught: ' + err));});
But this one does not (I only reversed the order):

CODE:

document.body.addEventListener('click', async function () {    async function usingThrow() {        throw Error('Using throw');    }    await usingThrow().catch(err => console.log('Caught: ' + err));    async function usingPromiseReject() {        return Promise.reject('Using Promise.reject');    }    await usingPromiseReject().catch(err => console.log('Caught: ' + err));});
Thus, I suspect there may be some race condition.

Additionally:
  • The error happens only when using an event handler. There is no error if directly executing the code.
  • For the code causing errors, sometimes there is no error on the first clicks.
As a reminder, in async functions, throws are converted to promise rejects. And in await calls, promise rejects are converted to exceptions. :P

Maybe a some point in the await call, the promise reject is converted to a throw, whereas it shouldn't be, as it is handled by the .catch(). And that "leftover throw" doesn't break execution, while still being logged.

Viewing all articles
Browse latest Browse all 2686

Latest Images

Trending Articles



Latest Images