Microtask queues in JavaScript

Saurabh Mhatre
3 min readMay 1, 2023

--

Article title

In JavaScript, microtask queues refer to a mechanism that the JavaScript engine uses to manage the execution of certain types of asynchronous code.

Microtasks are typically executed immediately after the current JavaScript execution context is finished, but before the next rendering or user input event is processed.

Microtask queues are typically used to manage asynchronous code that needs to be executed quickly, such as Promises and certain types of browser APIs (e.g. MutationObserver). When a microtask is created, it is added to the microtask queue, which is a first-in, first-out (FIFO) queue. Once the current JavaScript execution context is finished, the microtask queue is emptied, executing all microtasks in the order in which they were added.

In JavaScript, when you execute a piece of code, it is executed synchronously. This means that each line of code is executed one after the other, and the next line of code doesn't execute until the current line has finished executing. However, in modern JavaScript, many tasks are executed asynchronously. Asynchronous tasks are tasks that take some time to complete, but don't block the execution of the rest of the code. Examples of asynchronous tasks in JavaScript include fetching data from a server, reading a file, or waiting for user input.

JavaScript provides two mechanisms for executing asynchronous code: callbacks and Promises. Callbacks are functions that are called when an asynchronous task is complete. Promises, on the other hand, are objects that represent a value that might not be available yet. Promises provide a way to handle asynchronous code in a more readable and maintainable way than using callbacks.

When a Promise is created and executed, it doesn't execute immediately. Instead, it is added to the microtask queue. Microtasks are a type of asynchronous code that is executed as soon as possible, after the current execution context is finished, but before any rendering or user input events are processed. This makes microtasks useful for code that needs to execute quickly and doesn't require user interaction or rendering.

When the current execution context is finished, the JavaScript engine checks the microtask queue. If the microtask queue is not empty, the JavaScript engine starts executing the microtasks in order. Once all the microtasks in the queue have been executed, the JavaScript engine checks the task queue for any new tasks to execute.

Here's an example to illustrate how microtask queues work in JavaScript:

console.log('Start');

Promise.resolve().then(() => console.log('Promise 1'));
Promise.resolve().then(() => console.log('Promise 2'));

console.log('End');

In this example, two Promises are created and added to the microtask queue. The console.log statements are executed immediately, so the output will be:

Start
End
Promise 1
Promise 2

Note that the Promises are executed after the current execution context is finished, but before any rendering or user input events are processed. This ensures that the Promises are executed quickly, without causing any delays in the rest of the JavaScript code.

In summary, microtask queues are a way of managing the execution of certain types of asynchronous code in JavaScript. By using microtask queues, JavaScript can execute asynchronous code quickly and efficiently, without delaying the execution of other code or causing the user interface to freeze.

--

--

Saurabh Mhatre
Saurabh Mhatre

Written by Saurabh Mhatre

Senior Frontend Developer with 9+ years industry experience. Content creator on Youtube and Medium. LinkedIn/Twitter/Instagram: @SaurabhNative

No responses yet