MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Microtasks vs Macrotasks (Event Loop Deep Dive)0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
4.1Microtasks vs Macrotasks (Event Loop Deep Dive)4.2Custom Promise Implementation4.3Async Generators & For-Await-Of4.4Web Workers & Multithreading in JS
HomeTutorialsJavaScript for AdvancedMicrotasks vs Macrotasks (Event Loop Deep Dive)
Custom Promise Implementation

Microtasks vs Macrotasks (Event Loop Deep Dive)

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Mastering the JavaScript Event Loop requires understanding the boundary between the Microtask Queue (Job Queue) and the Macrotask Queue (Task Queue). Misunderstanding task scheduling leads to subtle race conditions, delayed rendering frames, and interface freezes.


1. The Two Asynchronous Queues

JavaScript categorizes asynchronous callbacks into two distinct queues with vastly different execution priorities:

1┌──────────────────────────────────────────────────────────────┐
2│ THE EVENT LOOP CYCLE │
3├──────────────────────────────────────────────────────────────┤
4│ 1. Execute one MACROTASK from the Task Queue │
5│ 2. Execute ALL MICROTASKS in the Microtask Queue │
6│ (until the microtask queue is COMPLETELY EMPTY!) │
7│ 3. Perform UI Render / Repaint (if necessary, ~16ms/60fps) │
8│ 4. Repeat cycle! │
9└──────────────────────────────────────────────────────────────┘
Queue TypeAPIs & TriggersDrain Behavior
Microtask QueuePromise.then/catch/finally, queueMicrotask(), process.nextTick (Node), MutationObserverDrained completely to zero items before moving to rendering or next macrotask
Macrotask QueuesetTimeout, setInterval, setImmediate (Node), I/O events, UI clicks/keyboard eventsExecutes one single task, then cedes control to microtasks and rendering

2. Execution Order Tracing

Analyze this classic interview test case:

1console.log('1. Script Start (Sync)');
2 
3setTimeout(() => {
4 console.log('2. setTimeout (Macrotask)');
5}, 0);
6 
7Promise.resolve().then(() => {
8 console.log('3. Promise 1 (Microtask)');
9}).then(() => {
10 console.log('4. Promise 2 (Microtask)');
11});
12 
13queueMicrotask(() => {
14 console.log('5. queueMicrotask (Microtask)');
15});
16 
17console.log('6. Script End (Sync)');

Execution Step-by-Step:

  1. 1
    Synchronous code runs first:
  • Logs 1. Script Start (Sync)
  • Schedules setTimeout callback in Macrotask Queue.
  • Schedules Promise 1 in Microtask Queue.
  • Schedules queueMicrotask in Microtask Queue.
  • Logs 6. Script End (Sync)
  1. 1
    Call Stack is now empty! Event Loop checks Microtask Queue:
  • Runs Promise 1 -> logs 3. Promise 1 (Microtask) -> schedules Promise 2 in Microtask Queue!
  • Runs queueMicrotask -> logs 5. queueMicrotask (Microtask).
  • Runs Promise 2 -> logs 4. Promise 2 (Microtask).
  • Microtask Queue is now completely empty.
  1. 1
    Event Loop checks Macrotask Queue:
  • Runs setTimeout callback -> logs 2. setTimeout (Macrotask).

Final Console Output Order: 1 -> 6 -> 3 -> 5 -> 4 -> 2


3. Microtask Starvation (UI Freeze)

Because the engine must drain the entire Microtask Queue before rendering or moving to the next macrotask, recursively scheduling microtasks will freeze the browser completely:

1// DANGER: Freezes browser tab completely! UI never repaints!
2function infiniteMicrotask() {
3 Promise.resolve().then(infiniteMicrotask);
4}
5// infiniteMicrotask(); // Call stack never yields to UI paint!

4. When to Use queueMicrotask()

The queueMicrotask() standard API allows you to schedule a function to run asynchronously after the current synchronous function finishes, but before browser rendering or timer macrotasks:

1function trackTelemetry(event) {
2 // Enqueue telemetry without blocking current synchronous work,
3 // but ensure it executes before the next render frame:
4 queueMicrotask(() => {
5 sendBeacon('/analytics', event);
6 });
7}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the execution priority between the Microtask Queue and the Macrotask Queue?
Question 2
Which of the following schedules a callback in the Microtask Queue?
Question 3
What happens if microtasks recursively enqueue new microtasks indefinitely?
Question 4
How many macrotasks does the Event Loop execute before checking the microtask queue?
Question 5
What standard Web API allows developers to explicitly enqueue a callback into the microtask queue without creating a dummy Promise?
Next Lesson

Custom Promise Implementation

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Top-Level Await in ES ModulesCustom Promise Implementation

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext