The function in your timeout gets queued as a task. The script runs and once it is done (console.log('third')), the engine can handle the task queue and will execute the timeout callback. So, even though the timeout is zero, the function will not get called immediately.
There is a lot more to the topic and Jake Archibald wrote an amazing article about how this works. I highly recommend reading it:
The function in your timeout gets queued as a task. The script runs and once it is done (
console.log('third')
), the engine can handle the task queue and will execute the timeout callback. So, even though the timeout is zero, the function will not get called immediately.There is a lot more to the topic and Jake Archibald wrote an amazing article about how this works. I highly recommend reading it:
jakearchibald.com/2015/tasks-micro...
Thank you, Oscar 🤜