0% found this document useful (0 votes)
51 views9 pages

Event Loop Js 1711642946

The Event Loop in JavaScript is a concurrency model that allows for the execution of multiple tasks despite JavaScript being single-threaded. It consists of four main components: Call Stack, Web APIs, Task Queue, and Micro Task Queue, which manage the execution order of functions and handle asynchronous operations. When the Call Stack is empty, tasks from the Micro Task Queue are prioritized for execution before those in the Task Queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

Event Loop Js 1711642946

The Event Loop in JavaScript is a concurrency model that allows for the execution of multiple tasks despite JavaScript being single-threaded. It consists of four main components: Call Stack, Web APIs, Task Queue, and Micro Task Queue, which manage the execution order of functions and handle asynchronous operations. When the Call Stack is empty, tasks from the Micro Task Queue are prioritized for execution before those in the Task Queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
Event Loop in JavaScript What is Event Loop? JavaScript is single threaded. It can execute one task at a time. But when building sites, we can execute multiple tasks at a time, for instance, listening for events, timeouts, etc. JavaScript does this with the Event Loop. It is a concurrency model that has four major components, i.e., Call Stack (which follows Stack DS), Web APIs, Task Queue, and Micro Task Queue. & i EteM lal} an le Wa) Parts of Event Loop (Part 1) 1. Call Stack: A stack data structure (LIFO) that is used for keeping track of currently executing function calls. 2. Web APIs: When web APIs like setTimeout or DOM manipulation functions gets in the Call Stack, they are moved to separate bucket known as Web APIs. When the move is done, the setTimeout() functions pops from the Call Stack so Call Stack can execute next line of code. Saad Irfan ce eed lle) ret Laop in Taasergt Po Saad Irfan jp lei Te] Parts of Event Loop (Part II) 3. Task Queue: When the setTimeout() delay finishes, the Web API moves the timeout to the Task Queue. Task Queue will wait for the Call Stack to get empty. Once it is empty, it will remove the first task from the Queue and moves it on the Call Stack to execute that code (for instance, function inside setTimeout()). 4. Micro Task Queue: Another Queue that has higher priority than Task Queue but it holds Promises instead of Web APIs. It follows the same principle of pushing task in it to Call Stack when Call Stack is empty. 7 Eto lal} ra ps le MIC) Po Saad Irfan jp NEL ins Le] Explanation 1. setTimeout() moved to Web APIs and removed from Call Stack. Web APIs executed the delay and moved the function to Task Queue. 2. Promise moved the function inside it to Micro Task Queue. 3. When Call Stack is empty, functions in Micro Task Queue are move to the Call Stack one at a time to execute. 4. When Micro Task Queue is empty, functions in Task Queue move to Call Stack one at a time to execute. é Saad Irfan a Ds Soret) é Saad Irfan EL ins Le] coll Stack ( Micro Tok Queue Py Saad Irfan pe ine]

You might also like