The mechanism built into JavaScript that manages the asynchronous execution of code.
- First, synchronous code is executed.
- If asynchronous functions are called, they are added to the queue. They are executed one after the other when the synchronous code they were called from finishes computation.
A good example illustrating the Event Loop in action is the setTimeout
function. It schedules a task to be added to the queue after a specified time. Therefore, there's no guarantee of executing the function at the exact time provided as an argument, but it is guaranteed not to be executed earlier. If synchronous code takes longer than the time set for the function to run, the asynchronous code must wait.
For more information, you can check here.