JavaScript Event Loop, Microtasks, and Macrotasks Explained Visually with Clear Examples and Execution Order

JavaScript Event Loop, Microtasks, and Macrotasks Explained Visually with Clear Examples and Execution Order


The Event Loop, Microtasks, and Macrotasks Explained Visually



1️⃣ What Is the Event Loop?

JavaScript is single-threaded, meaning it can do one thing at a time. But with asynchronous code, it handles waiting tasks (e.g., timeouts, fetch) using the event loop.

📌 Key Components:

  • Call Stack (executes code)

  • Web APIs (browser-side: timers, fetch, etc.)

  • Task Queues (for microtasks and macrotasks)

  • Event Loop (glue that checks if the call stack is empty)

The event loop constantly checks the call stack and pushes tasks from the queues when it's empty.


 

2️⃣ Microtasks vs Macrotasks: What’s the Difference?

🔹 Microtasks:

  • Promise.then(), catch(), finally()

  • queueMicrotask()

🔹 Macrotasks:

  • setTimeout(), setInterval()

  • setImmediate() (Node.js)

  • UI rendering, I/O tasks

🧠 Microtasks are always executed before macrotasks once the call stack is clear.

3️⃣ Visual Execution Flow Example


console.log("Start"); setTimeout(() => { console.log("Macrotask"); }, 0); Promise.resolve().then(() => { console.log("Microtask"); }); console.log("End");

Execution Order:

  1. "Start"

  2. "End"

  3. "Microtask"

  4. "Macrotask"

Why?

  • console.log("Start") and console.log("End") run first (synchronous).

  • Then microtask queue is checked → "Microtask".

  • Then macrotask queue → "Macrotask".



4️⃣ Practical Scenarios Where Order Matters

✅ Handling async data


fetch("/data") .then(() => console.log("Promise resolved")); console.log("After fetch");

"After fetch" logs before "Promise resolved"

✅ Nested Promises & Microtasks


Promise.resolve().then(() => { console.log("Microtask 1"); Promise.resolve().then(() => { console.log("Nested Microtask"); }); });

Output:

  • Microtask 1

  • Nested Microtask (queued after 1st finishes)


5️⃣ Common Mistakes and Best Practices

❌ Mistake: Assuming setTimeout(..., 0) executes immediately
✅ Fact: It goes to macrotask queue and waits for the stack & microtasks to clear

❌ Mistake: Overusing long synchronous code that blocks the event loop
✅ Solution: Break heavy tasks into smaller async chunks using setTimeout(fn, 0) or requestIdleCallback()

✅ Use Promise.resolve() to prioritize code with microtask scheduling






This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob

Previous Post Next Post