Skip to content
Shubham JangleResources
JavaScript

Explore topics

Event loop

Watch the walkthrough
Open on Instagram

Revision clarifications

The example logs A, D, C, B. Promise reactions are microtasks; a zero-delay timer is a later task, not immediate execution. Microtasks are drained at checkpoints before another task; recursively queuing them can also delay rendering. Each worker has its own execution context, so browser JavaScript can run in multiple agents.

The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.

Original HackTheCode notes

Read the original Markdown

Event loop

  • Did you know that JavaScript is single-threaded? This means it can only do one thing at a time.
  • But how does JavaScript handle multiple tasks, like making asynchronous calls and rendering pages smoothly? That's where the Call Stack & Event Loop comes into play!
  • Let's start with the Call Stack. The Call Stack is stack data structure which means its first in last out. When a function is called, it's added to the top of the stack. Once it's done, it's removed from the stack.
  • But what happens when you make an asynchronous call, like fetching data from an API? These calls are handled by another component like "Web APIs" (Browser) or "libuv library" (Node.js).
  • Once they finishes their task (except promises), the callback is sent to the Event Queue, also known as the Task Queue.
  • But Promises are handled with a higher priority. They go to the Micro-task Queue (Priority Queue), which ensures they are processed faster than tasks in the Event Queue.
  • The Event Loop is the hero here. It keeps an eye on the Call Stack. When the Call Stack is empty, the Event Loop takes the next callback from the Event Queue or the Micro-task Queue and pushes it onto the Call Stack.
  • Remember, blocking the Call Stack is a bad idea because it also handles rendering tasks as well. If the Call Stack is - blocked, your page can get stuck.
function logA() {
  console.log("A");
}
function logB() {
  console.log("B");
}
function logC() {
  console.log("C");
}
function logD() {
  console.log("D");
}

logA();
setTimeout(logB, 0);
Promise.resolve().then(logC);
logD();

Click here to visualize the above example.