🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #226

Async Event Loop

JavaScript can only execute one piece of code at a time.

While JavaScript is busy, other JavaScript code must wait.

How do JavaScript handle timers, user clicks, downloads, and asynchronous operations?

The answer is the Event Loop.

Understanding the Event Loop

Understanding the Event Loop explains many common JavaScript behaviors.

  • Why setTimeout(..., 0) does not execute immediately.
  • Why long loops freeze a page.
  • Why Promises execute before timers.
  • Why await does not block the browser.
  • Why click events wait until current code has finished.

JavaScript Executes One Task at a Time

JavaScript executes code in sequence.

Only one function can execute at any given moment.

Functions execute in the order they are called.

Each function must finish before the next one can begin.

Examples

function step1() {

  myDisplayer("Step 1");

}

function step2() {

  myDisplayer("Step 2");

}

step1();

step2();

The Browser Performs Asynchronous Work

JavaScript itself does not wait for timers or download web pages.

Instead, the browser performs these operations while JavaScript continues running.

Examples include:

  • Timers
  • Network requests
  • User events
  • File loading

When an operation finishes, JavaScript is notified that the result is ready.


The Event Loop

The Event Loop continuously checks whether JavaScript is busy.

When JavaScript finishes its current task, the Event Loop starts the next waiting task.

This process repeats for as long as the page is open.

JavaScript starts

↓

Run code

↓

Start timer

↓

Continue running

↓

Timer finishes

↓

Event Loop

↓

Run callback

Note: The Event Loop coordinates asynchronous operations and decides when JavaScript should execute the next task.


Example: setTimeout()

The callback is not executed immediately.

Instead, it waits until JavaScript finishes its current work.

Example

myDisplayer("A");

setTimeout(function() {

  myDisplayer("B");

}, 2000);

myDisplayer("C");

The timer finishes almost immediately.

However, the callback cannot run until JavaScript finishes executing the current code.

The Event Loop starts the callback after the current task has completed.


Example: Promise

Example

myDisplayer("A");

Promise.resolve().then(function() {

  myDisplayer("B");

});

myDisplayer("C");

Why did the Promise callback run before the timer?

The answer is that Promise callbacks are placed in a special queue with higher priority.


Task Queue and Microtask Queue

JavaScript uses two main queues for asynchronous work.

Col 1 Col 2
Queue Contains
Task Queue Timers, events, messages
Microtask Queue Promise handlers and awai

After each task finishes, JavaScript processes all waiting microtasks before starting the next task.


Comparing Timers and Promises

Col 1 Col 2
Operation Queue
setTimeout() Task Queue
setInterval() Task Queue
Click event Task Queue
Promise.then() Microtask Queue
catch() Microtask Queue
finally() Microtask Queue
await continuation Microtask Queue

Long Tasks Block JavaScript

If JavaScript executes a long-running task, everything else must wait.

During this time, the browser cannot execute other JavaScript code.

User clicks, timers, and completed downloads must wait until the current task finishes.

Example

function wait() {

  let i = 4e9;

  while (--i > 0);

}

myDisplayer("Start");

wait();

myDisplayer("Done");

Example Explained

Note: Why did the example take a long time? Why was "Start" and "Done" diplayed at the same time? The questions lead directly to how browsers update the page.

Example

myDisplayer("Start");

wait();

myDisplayer("Done");

Step 1

myDisplayer("Start");

Step 2

wait();

Step 3

myDisplayer("Done");

Step 4

Start Done

The Timeline

JavaScript starts

↓

myDisplayer("Start")

(DOM changes)

↓

wait()

(JavaScript busy)

↓

myDisplayer("Done")

(DOM changes again)

↓

JavaScript finishes

↓

Browser repaints

↓

Screen shows:

Start Done

Note: The example above is a good Event Loop example. It demonstrates that there are two separate things: Updating the DOM Updating the screen (rendering) The DOM is updated immediately, but the browser must wait until the current JavaScript task has finished before repainting.


Want to show "Start" first?

Example

function wait() {

  let i = 4e9;

  while (--i > 0);

}

myDisplayer("Start");

setTimeout(function () {

  wait();

  myDisplayer("Done");

}, 0);

The Timeline

JavaScript starts

↓

Display "Start"

↓

Current task finishes

↓

Browser repaints

(Start becomes visible)

↓

Event Loop

↓

Run wait()

↓

Display "Done"

↓

Browser repaints

Summary

  • JavaScript executes one task at a time.
  • The browser performs asynchronous operations such as timers, downloads, and user events.
  • Completed operations wait until JavaScript is ready.
  • The Event Loop starts waiting tasks when the current task has finished.
  • Promise handlers and await continuations run before timers because they use the Microtask Queue.

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Async Event Loop – FAQs

Quick answers about learning Async Event Loop in JavaScript.

This free note from CodingNow 2.0 explains Async Event Loop in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including Async Event Loop, is 100% free with no signup required.
With focused practice, most students grasp Async Event Loop in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now