Apparently the non async code inside an async
function will execute synchronously even if you don't await
.
Consider this code example:
async function f() {
g('adsasdasd'); // note that it is not `await`ed
console.log('000');
}
async function g(m) {
console.log('g', m);
}
f() // calling `f`
I always thought it would produce this output:
000
g adsasdasd
BUT, the actual output is like this
g adsasdasd // function `g` is executed first!!
000
Can anyone explain this?
Top comments (1)
The return value of an
async
function is always anPromise
. Here functiong
is technically returningundefined
, but before that its executingconsole.log
which is a simple log statement. Hence it prints the string. The subsequent call toconsole.log(000)
prints another value.