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.
setTimeoutis to do something later in time, hence asynchronous.1; Print2after 2 seconds; Print3" That's exactly what you get -1->3-> 2 second wait ->2. And async functions also are...async. Halting the thread because you've called a synchronous function in an async one also makes no sense. You are better off reading this, this, and this then asking about a concrete problem.