This question is very similar in intent to Difference between microtask and macrotask within an event loop context, but more specific, in that it asks for the explication of a definite example: I think for this reason it shouldn't be considered a duplicate.
What is the state Macrotask queue and Microtask queue during the execution of this code in node.js
console.log("A1");
(async ()=> {
console.log("1")
f = async ()=>{console.log('2')}
await f()
console.log("3")
})()
console.log("A2");
Output:
A1
1
2
A2
3
Output I expected: A1, A2, '1','2','3'
Based on this reasoning: log A1 --> enqueue anonymous function on microtask queue --> log A2 --> execute anaonymous function log 1, enqueue f on the microtask queue --> execute f from microtask queue --> log 2 --> log 3
Where am I going wrong? (additionally how is a top level async func enqueued?)
NOTE: actual command used to run this was npx babel-node myscript.js