DEV Community

Cover image for Event Loop: Call Stack, Web API, Task Queue, Microtask Queue
Antonov Mike
Antonov Mike

Posted on

Event Loop: Call Stack, Web API, Task Queue, Microtask Queue

JavaScript runs synchronously. The Event Loop enables efficient management of asynchronous operations, providing non-blocking code execution. Understanding how the Event Loop, the call stack, the task queue, and the microtask queue work helps developers write more efficient and responsive web applications.

The Event Loop is a mechanism in JavaScript that allows asynchronous processing of events and tasks, ensuring non-blocking code execution. This is especially important for web applications, where the user interface must remain responsive even when long-running operations are performed.

Main components of the Event Loop:

  1. Call Stack: This is a data structure that keeps information about the currently executing function calls. When a function is called, it is pushed onto the stack, and when it finishes execution, it is popped off the stack. If the call stack becomes empty, it means all synchronous tasks have completed.
  2. Web APIs: These are a set of built-in functions and methods provided by the browser or the JavaScript runtime environment (e.g., Node.js). Examples include setTimeout, setInterval, fetch, DOM events, etc. When an asynchronous operation is invoked, it is handed off to the corresponding Web API, which performs the operation and places the result into the task queue.
  3. Task Queue: This is a queue where tasks that are ready to run are placed. When an asynchronous operation completes, its callback is added to the task queue. Tasks in the task queue are executed in order when the call stack is empty.
  4. Microtask Queue: This is a separate queue for microtasks such as Promise callbacks and MutationObserver. Microtasks have higher priority compared to tasks in the task queue and are executed before them. ### Example of the Event Loop in action
console.log('-> 1');
setTimeout(() => console.log('----> 2'));
Promise.resolve().then(() => console.log('---> 3'));
console.log('--> 4');
Enter fullscreen mode Exit fullscreen mode

Step-by-step explanation:

  1. Synchronous operations: console.log('-> 1') runs and is processed on the call stack. setTimeout() is handed to a Web API and control returns immediately. Promise.resolve().then is scheduled in the microtask queue. console.log('--> 4') runs and is processed on the call stack.
  2. Microtask Queue: When the call stack is empty, the Event Loop checks the microtask queue. Promise.resolve().then runs (pushed to the call stack), then console.log('---> 3') executes.
  3. Task Queue: After all microtasks have finished, the Event Loop checks the task queue. The setTimeout() callback is placed in the task queue. console.log('----> 2') runs. ### Execution result:
-> 1
--> 4
---> 3
----> 2
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Understanding the Event Loop and its related components—the call stack, Web APIs, the task queue, and the microtask queue—is essential for building responsive and efficient JavaScript applications. The Event Loop provides non-blocking execution: synchronous operations run first, microtasks (Promise, MutationObserver) are handled immediately after, and then macrotasks (setTimeout, I/O, etc.) run afterward. Knowing the priorities and execution order allows you to predict asynchronous behavior, avoid race conditions and UI blocking, and write more stable and performant code.

Makhabat Abdisattarova Как работает Event Loop в JavaScript? Микрозадачи, Очереди задач и Web API | JavaScript

Как работает Event Loop в JavaScript? Микрозадачи, Очереди задач и Web API | JavaScript

Top comments (0)