After running the code below, I noticed an interesting behavior:
setTimeout(() => console.log('first'), 0)
console.log('second');
As expected in JavaScript's asynchronous nature, the output was as follows:
second
first
However, when I modified the first argument of the setTimeout()
function like this:
//notice the ()=> part is removed
setTimeout(console.log('first'), 0)
console.log('second');
The order of output changed to:
first
second
It seems that by removing the ()=>
part, JavaScript started behaving synchronously again.
Any insights on this unexpected result?