JavaScript Event loop

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

JavaScript runs on a single call stack. Asynchronous work is queued: promise jobs (microtasks) run before the next timer/task. That scheduling system is the event loop.

JavaScript Syntax

JavaScript
console.log("A");
setTimeout(() => console.log("C"), 0);
Promise.resolve().then(() => console.log("B"));
Try it Editor

How It Works

  1. A. Synchronous log.
  2. B. Microtask from the promise.
  3. C. Timer task runs after microtasks.

Examples

Example 1 — Nested promises

Microtasks drain fully.

JavaScript
Promise.resolve().then(() => {
  console.log(1);
  Promise.resolve().then(() => console.log(2));
});
setTimeout(() => console.log(3), 0);
Try it Editor

How It Works

  1. Both promise callbacks before the timer.

Example 2 — await continues as microtask

After await resumes later.

JavaScript
(async () => {
  console.log("start");
  await null;
  console.log("after");
})();
console.log("sync");
Try it Editor

How It Works

  1. await yields; sync finishes; then resume.

Example 3 — Why UI stays responsive

Long sync work blocks.

JavaScript
console.log("heavy sync work blocks the stack");
Try it Editor

How It Works

  1. Keep CPU work short or split it.

Frequently Asked Questions

No. It waits until the stack is clear and microtasks finish.
Their promise reactions are microtasks after the response is ready.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful