By design, an uncaught error thrown in an async
function will reject the promise that the async
function returns. Quoting Mozilla Contributors:
Return value
A Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function.
So at the time the exception is thrown, it is caught by the async function and dealt with to settle the promise it returns. The exception has been handled (not postponed), and the async function returns after which execution happily continues with console.log('test')
.
There is however another mechanism whereby the host will trigger an "unhandled promise rejection" event when a rejected promise has no rejection handler, which is the case in your code. This may look like your exception was delayed, but it is in fact a different mechanism which repeats the reason that the concerned rejected promise has. This event will only be triggered when the callstack is empty and it is clear that the rejected promise has no rejection handler.