The delay in setTimeout is not the exact timer at which its callback function will get executed. The timer of setTimeout is a threshold, not an absolute, and is the minimum time after which the callback function can be executed. When the time reaches, it will be scheduled and executed as soon as possible. In the case of JavaScript and Event Loop, it will be scheduled in the task queue. When the call stack is free and there are no callbacks to be executed, the setTimeout callback will get executed. It is important to note that the timer is the first phase of the event loop So, when the callbacks are executed, the poll phase occurs, and timer callbacks are promoted from the task queue to the call stack, and it gets executed. So, as shown in the video, even though setTimeout was to be executed after 1 second, it gets done after 10 seconds. I do a deep dive into foundational concepts & how things work under the hood. Consider connecting with or following me, Ali Raza, to get along with the journey. #nodejs #javascript #react
Exactly. You may cover micro and macro task queues in next post, which will make this video more cooler that how the tasks are getting picked. :)
Very true and also microtasks have priority over the task queue even if the call stack is empty the event loop won't execute the timer until all microtasks are executed
I have recently started writing on X. Mind connecting? https://x.com/thataliraza