DEV Community

Kanha Kumar Khatua
Kanha Kumar Khatua

Posted on

Event Loop javascript

Event Loop:-

The event loop is a mechanism in JavaScript that allows the execution of asynchronous code. It is a single-threaded loop that constantly checks for events and then executes the appropriate callback functions.

Hoisting:-

Hoisting refers to the process where the interpreter appears to move the declaration of function, variables, or classes to the top of their scope, period, and execution of the code.

Image description

  • When JS code runs it creates a global execution context and pushes it into the call stack and after the complete execution of the code it pops out from the call stack.
  • If any WEB APIs come then it registers the timer web API or Browser after the timer is expired it pushes into the micro-task queue(high priority, Promises, async/await) or call-back queue (setTimeout, setInterval).
  • Event loop watch the call stack if the call stack is empty then it pushes the task from the micro-task queue or call-back/task queue.

Starvation:-

It happens because in the micro-task queue if one executes and creates another call-back function it continuously happens and the micro-task is not free due to this higher priority, it runs again and again whenever the call stack is free and there is no chance of the call-back queue to run, due to this call-back queue going into starvation.

Top comments (0)