Types of javaScript tasks queues.

devfaysalkhan
3 min readJun 22, 2023

--

In JavaScript, there are two types of task queues: the microtask queue and the macrotask queue.

javascript tasks faysal khan dev

Event Loop: JavaScript is a single-threaded language, meaning it can only execute one task at a time. However, it incorporates an event-driven, non-blocking I/O model to handle asynchronous operations efficiently. To manage the execution of these asynchronous tasks, JavaScript uses a mechanism called the event loop. For example I use setTimeOut() web api.

Call Stack: The call stack is a data structure that keeps track of the currently executing task. Whenever a function is called, it is added to the call stack, and when a function completes its execution, it is removed from the stack.

Tasks queues: Task queues, also known as task lists or message queues, are used to store tasks that are not immediately executed but need to be processed by the JavaScript runtime. There are two main types of task queues in JavaScript: the microtask queue and the macrotask queue.

  1. Microtask Queue: The microtask queue, as the name suggests, handles microtasks. Microtasks have higher priority and are executed immediately after the currently executing task, but before the next task is picked up from the event queue. This ensures that microtasks are processed as soon as possible.

Microtasks are commonly used for tasks that require immediate attention or have important synchronization requirements. Examples of microtasks include:

#Promises: When a Promise is resolved or rejected, the respective callbacks are added to the microtask queue.

  • queueMicrotask: This function allows you to schedule a microtask.
  • process.nextTick: In Node.js, this function schedules a callback to be executed after the current operation completes but before the event loop continues.

2. Macrotask Queue: The macrotask queue handles macrotasks, which are executed at a lower priority compared to microtasks. Macrotasks are picked up from the event queue only when there are no microtasks pending execution. This ensures that macrotasks do not block critical operations and keep the application responsive.

Macrotasks are typically used for tasks that involve I/O operations, timers, user interactions, or other asynchronous operations. Examples of macrotasks include:

  • setTimeout and setInterval: These functions schedule tasks to be executed after a specified delay or at regular intervals.
  • setImmediate: In Node.js, this function schedules a callback to be executed in the next iteration of the event loop.
  • requestAnimationFrame: This method schedules a function to be executed before the next repaint of the browser, typically used for smooth animations.
  • I/O operations: Tasks related to reading from or writing to files, making network requests, or interacting with databases.
  • UI rendering: Tasks involved in rendering and updating the user interface.

To summarize the process, when an asynchronous task is encountered, such as a timer, a promise, or an I/O operation, it is placed in the corresponding task queue (either the microtask queue or the macrotask queue). Once the call stack is empty, the event loop checks the microtask queue first and executes all the microtasks present. After the microtask queue is empty, the event loop proceeds to pick up a task from the macrotask queue and executes it. This cycle continues as long as there are tasks in the queues.

By utilizing separate task queues for microtasks and macrotasks, JavaScript ensures that critical tasks, such as Promises and other synchronization-related operations, are executed promptly. Meanwhile, macrotasks handle less time-sensitive operations, such as I/O and UI rendering, without blocking the execution of more critical tasks.

Happy coding ! and also check my #linkedIn and another article.

--

--

devfaysalkhan
devfaysalkhan

Written by devfaysalkhan

I am React and JavaScript Developer. I'm obsessed with solving complex problems using React, MongoDB, MS SQL, and Node.js by building web application.

No responses yet