Event Queue in JavaScript
Last Updated :
15 Apr, 2025
JavaScript, being single-threaded, processes tasks sequentially, meaning it executes one task at a time. This can pose a challenge when dealing with operations that take time to complete, such as fetching data from a server or performing complex calculations. To handle such scenarios efficiently, JavaScript employs asynchronous behavior.
The Async JavaScriptCall Stack
The call stack is a mechanism that JavaScript uses to keep track of its execution context. Whenever a function is invoked, a corresponding frame is pushed onto the call stack. This frame contains information about the function's arguments, local variables, and the line of code currently being executed. Once a function completes its execution, its frame is popped off the stack.
Asynchronous Behavior
Asynchronous behavior allows JavaScript to execute non-blocking code. This means that instead of waiting for a time-consuming operation to complete, JavaScript can continue executing other tasks while waiting for the result. Common examples of asynchronous operations include fetching data from an API, reading files, or executing setTimeout.
When an asynchronous operation is encountered, it is off loaded to the browser's APIs (such as XMLHttpRequest, setTimeout, or fetch) to handle. JavaScript continues executing other tasks in the meantime.
Event Queue and Event Loop
Event queue and Event loopWhile JavaScript is busy executing other tasks, the asynchronous operation is carried out by the browser in the background. Once the operation is completed, its result is placed in the event queue.
The event loop continuously monitors the call stack and the event queue. When the call stack is empty (i.e., there are no pending synchronous tasks), the event loop picks the first item from the event queue and pushes it onto the call stack for execution. This process ensures that asynchronous tasks are executed in the order they were completed, without blocking the main thread. So the asynchronous function like setTimeout in this case is executed after all synchronous code.
Example: To demonstrate the asynchronous nature of the JavaScript using the setTimeut.
JavaScript
console.log("First part")
setTimeout(() => {
console.log("Second part")
}
, 500)
//waits for 0.5s (Asyncronous code)
console.log("Third part")
OutputFirst part
Third part
Second part
Example: To demonstrate the asynchronous nature of the JavaScript using the setTimeout method.
JavaScript
console.log("First part")
setTimeout(() => {
console.log("Second part")
}
, 0)
//waits for 0s(Asyncronous code)
console.log("Third part")
OutputFirst part
Third part
Second part
The result of the asnychronous part was available immediately but the output is printed last. It is because all the asynchronous code is executed after all syncronous code.
Example: To demonstrate the working of the Asynch JavaScript in event loop.
JavaScript
console.log("First part")
setTimeout(() => {
console.log("Second part")
}
, 0)
//waits for 0s(Asyncronous code)
setTimeout(() => {
console.log("Second 2 part")
}
, 0)
//waits for 0s(Asyncronous code)
console.log("Third part")
OutputFirst part
Third part
Second part
Second 2 part
In JavaScript event queues, micro and macro task queues play crucial roles in managing asynchronous operations. Here's an overview of each:
Microtask Queue
Microtasks are tasks that are executed asynchronously, but right after the currently executing script. They are usually high-priority tasks and are often used for things like promises and mutation observers.
In JavaScript, the microtask queue is commonly implemented using the Promise object. When a promise settles (fulfilled or rejected), its respective .then() and .catch() handlers are placed in the microtask queue.
Example: To demonstrate the micro task queue working using the console.log('End') statement that comes after the promises, it's logged before the microtasks because microtasks execute immediately after the current task is done executing.
JavaScript
console.log('Start');
Promise.resolve().then(() => {
console.log('Microtask 1')
});
Promise.resolve().then(() => {
console.log('Microtask 2')
});
console.log('End');
OutputStart
End
Microtask 1
Microtask 2
Macro Task Queue
Macrotasks are tasks that are executed asynchronously, but they are placed at the end of the event queue and executed after the microtasks. Common examples of macrotasks include setTimeout, setInterval, and DOM event handlers. In JavaScript, the macro task queue includes tasks like setTimeout, setInterval, and I/O operations.
Example: To demonsrtate the working of the Macro task queue in JavaScript.
JavaScript
console.log('Start');
setTimeout(() => console.log('Macro task 1'), 0);
setTimeout(() => console.log('Macro task 2'), 0);
console.log('End');
OutputStart
End
Macro task 1
Macro task 2
Implementation in Event Queue:
The event loop in JavaScript handles both microtasks and macrotasks. When an event occurs, it's placed in the appropriate queue. Microtasks are executed first, followed by macrotasks.
Example: To demonstrate the working of the micro and macro task queue in JavaScript.
JavaScript
console.log('Start');
Promise.resolve().then(() => console.log('Microtask 1'));
setTimeout(() => console.log('Macro task 1'), 0);
console.log('End');
OutputStart
End
Microtask 1
Macro task 1
Micro tasks have higher priority and are executed before macro tasks in the JavaScript event loop. They are often used for critical operations like handling promises or observing mutations. Macro tasks, on the other hand, are deferred tasks that are executed after micro tasks and are commonly associated with I/O events and timers.
JavaScript
console.log("Start");
setTimeout(() => {
console.log("Inside setTimeout->1 (macrotask)");
}, 0);
Promise.resolve().then(() => {
console.log("Inside Promise.then->1 (microtask)");
});
Promise.resolve().then(() => {
console.log("Inside Promise.then->2 (microtask)");
});
setTimeout(() => {
console.log("Inside setTimeout->2 (macrotask)");
}, 0);
console.log("End");
OutputStart
End
Inside Promise.then->1 (microtask)
Inside Promise.then->2 (microtask)
Inside setTimeout->1 (macrotask)
Inside setTimeout->2 (macrotask)
The order of execution:
- "Start" is logged.
- Two setTimeout functions and two promise .then() functions are scheduled.
- "End of the script" is logged.
- Microtasks are executed. Both Promise.then() functions are executed in the order they were scheduled. So, "Inside Promise.then 1 (microtask)" and "Inside Promise.then 2 (microtask)" are logged.
- Macrotasks are executed. Both setTimeout functions are executed in the order they were scheduled. So, "Inside setTimeout 1 (macrotask)" and "Inside setTimeout 2 (macrotask)" are logged.
This demonstrates the execution order of tasks in both microtask and macrotask queues.
Similar Reads
JavaScript Events
JavaScript Events are actions or occurrences that happen in the browser. They can be triggered by various user interactions or by the browser itself. [GFGTABS] HTML <html> <script> function myFun() { document.getElementById( "gfg").innerHTML = "GeeksforGeeks"; } </
3 min read
JavaScript onmouse Events
The onmouse event is used to define the operation using the mouse. JavaScript onmouse events are: onmouseover and onmouseoutonmouseup and onmousedownonmouseenter and onmouseleave JavaScript onmouseover and onmouseout: The onmouseover and onmouseout events occur when the mouse cursor is placed over s
1 min read
Event Firing in JavaScript
What are Events?Events are actions or occurrences that happen in the system where we do programming, which the system tells you about so that we can respond to these events in some way if desired. For example, if the user selects a button on a webpage, one might want to respond to that action by dis
3 min read
Event Bubbling in JavaScript
Event bubbling in JavaScript is a mechanism where an event triggered on a child element propagates upward through its ancestors in the DOM. It allows parent elements to respond to events triggered by their child elements. Propagation Direction: In event bubbling, the event starts at the target eleme
6 min read
Phases of JavaScript Event
There are three different phases during the lifecycle of a JavaScript event. Capturing PhaseTarget PhaseBubbling Phase They follow the same order as listed above. Capturing Phase is when the event goes down to the element. The target phase is when the event reaches the element and the Bubbling phase
2 min read
How to trigger events in JavaScript ?
JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences
2 min read
JavaScript onclick Event
The onclick event generally occurs when the user clicks on an element. It's a fundamental event handler in JavaScript, triggering actions or executing functions in response to user interaction, facilitating dynamic and interactive web functionality. In JavaScript, we can use the onclick function in
2 min read
Event Delegation in JavaScript
Before learning about Event Delegation in JavaScript, we must be familiar with phases of JavaScript event https://www.geeksforgeeks.org/phases-of-javascript-event/ Event Delegation is basically a pattern to handle events efficiently. Instead of adding an event listener to each and every similar elem
3 min read
JavaScript Nested functions
A nested function (also known as an inner function) is a function that is declared within another function (known as the outer function). The inner function has access to the variables of its outer function, forming a lexical scope chain. [GFGTABS] JavaScript function outer() { console.log('This
5 min read
What are JavaScript Events ?
JavaScript Events are the action that happens due to the interaction of the user through the browser with the help of any input field, button, or any other interactive element present in the browser. Events help us to create more dynamic and interactive web pages. Also, these events can be used by t
7 min read