If the code is not clear I have provided it below,
It’s a pretty simple program which consists of two functions and where the second function is called from the first function. The first function is called initially and then it will pop in to the stack, from there, it will log “Hey, there!” to the console. After that It will pop the second function into the stack. Which in turn would call the setTimeout function.
This time it has ran the final console log and the stack is now empty except for the global execution context as usual. Now, the question is that, what has happened to the callback function inside our setTimeout.
After the timeout interval is over, the callback function will be sent to something called the message queue. In here, this will wait until it gets called. So, the function of event loop is to monitor both the stack and message queue constantly and if the stack is empty it will pick up the first callback function in the message queue and it will put it inside the stack. It will do this process until the message queue becomes empty.
In the next part, we will look at how Node JS implements this event loop.