简体   繁体   中英

Why throw is delayed in async functions

Async functions somehow postpone(schedule) throw statements from immediate execution, while normal statements like console.log are executed immediately.

 async function test() { console.log('before throw') throw 'error in test()' } test() console.log('exit')

I would expect test() to be executed till the end, so no 'exit' to be printed to the console.

Async functions somehow postpone(schedule) throw statements from immediate execution

No, they don't. The exception is thrown immediately when the function is called, and it does reject the promise returned by test() . A rejection which your code is ignoring, leading to an unhandled rejection !
Notice that the behaviour of throw is no different than if the statement was executed after the test code had await ed something.

I would expect test() to be executed till the end, so no 'exit' to be printed to the console.

If you don't want the 'exit' to be printed, put it inside the function after the throw statement:

 async function test() { console.log('before throw') throw 'error in test()' console.log('exit') } test()

By design, an uncaught error thrown in an async function will reject the promise that the async function returns. As 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM