The output is 1, 2 because
when the function increment()
is executed and await keyword is found in an async function, then whatever statement is after await starts acting as a Promise resolve, therefore the execution pauses over-there and moves to the line below increment()
call.
Thereby executing
num += 1;
console.log(num);
and logging 1.
When the call stack becomes empty the control returns back to await 2 and now the statement over there is
num = num + 2
the 2nd num over here still being 0, so 0 + 2 = 2.
Therefore 2 gets logged.