In this blog, we will learn what is event loop and how exactly it works.
- What is event loop ?
- Explanation :
For example :
Once all synchronous statements are executed, then asynchronous statements are executed.
But how exactly it works? Let's look at the flow of the execution:
all the codes you have typed are stored in callstack and executed in order. The first statement enter the callstack and executed. Then next Second statement enters the callstack.
Since the second statement is an asynchronous function, which takes time to execute is pushed to "event table". Event table is responsible for moving asynchronous codes to "Event Queue" after a specified time.
In the "event table" , setTimeOut() waits for 1 sec(for specified milliseconds), and after the time passes, "event table" pushes it to "event queue".
In the meantime, third statement enters the callstack and get executed.
"Event loop" continously tracks the callstack. After the last synchronous statements are executed within callstack, "event loop" will check if there is any codes/statements within "event queue".
if its present, then event loop will pass it onto callstack, where the asynchronous codes/statements are executed.
Top comments (0)