DEV Community

Discussion on: If Javascript Is Single Threaded, How Is It Asynchronous?

Collapse
 
owl profile image
Oscar • Edited

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...

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, Oscar 🤜