I have problem understanding how async function work in JavaScript. Let's take the code below:
async () => {
console.log(1);
setTimeout(() => console.log(2)
, 2000)
console.log(3);
}
I would expect that invocation of synchronous function inside async should block thread before executing further code. So I'd expect to get 1 -> 2 -> 3
instead I'm getting 1 -> 3 -> 2
. Can't find explanation why it's happening and how to block thread to receive output 1 -> 2 -> 3
.
I'm using Node 12 with serverless framework.