Microtask in JavaScript

Overview

Microtasks are an essential component of the JavaScript event loop, designed to handle smaller tasks that update the application state. They are executed after each macrotask and before the UI rendering, ensuring that the application state is consistent before the UI is updated. This mechanism allows for efficient and predictable updates to the application state, which is crucial for maintaining a smooth user experience.

Definition

A microtask is a smaller task within the event loop that is responsible for updating the application state. Common examples of microtasks include promise callbacks and DOM mutation changes. These tasks are executed as soon as possible, immediately following the completion of each macrotask. This prioritization ensures that the application state is updated promptly, which is critical for the correct functioning of the application.

Microtasks and Macrotasks

The event loop in JavaScript typically manages two types of task queues: the macrotask queue and the microtask queue. As depicted in Figure 13.1, the event loop first checks the macrotask queue and executes one macrotask at a time. Once a macrotask is fully processed, the event loop moves on to the microtask queue, executing all microtasks present in the queue before proceeding to the next macrotask. This distinction is crucial because, in a single loop iteration, only one macrotask is processed, whereas all microtasks are handled.

Figure 13.1. The event loop usually has access to at least two task queues: a microtask queue and a macrotask queue. Both types of tasks are handled one at a time. Figure 13.1. The event loop usually has access to at least two task queues: a microtask queue and a macrotask queue. Both types of tasks are handled one at a time.

Execution Priority

Microtasks have a higher priority than macrotasks when it comes to execution. For instance, if a microtask is queued, it will be processed even if an older macrotask is waiting in the queue. This behavior is illustrated in Figure 13.6, where a promise success microtask takes precedence over a secondButton click event task.

Figure 13.6. If a microtask is queued in the microtask queue, it gets priority and is processed even if an older task is already waiting in the queue. In this case, the promise success microtask gets priority over the secondButton click event task. Figure 13.6. If a microtask is queued in the microtask queue, it gets priority and is processed even if an older task is already waiting in the queue. In this case, the promise success microtask gets priority over the secondButton click event task.

Timing and Rendering

The timing of microtask execution is crucial for maintaining a consistent application state. As shown in Figure 13.8, after a macrotask is executed, the event loop processes all tasks in the microtask queue before moving on to the next macrotask. This ensures that the application state is updated before any rendering occurs.

Figure 13.8. After a task gets executed, the event loop processes all tasks in the microtask queue. In this case, before moving to the secondButton click task, the promise success task is handled. Figure 13.8. After a task gets executed, the event loop processes all tasks in the microtask queue. In this case, before moving to the secondButton click task, the promise success task is handled.

Figure 13.9 further illustrates that a page can be re-rendered between two macrotasks, but not before a microtask is executed. This behavior ensures that microtasks, such as promise handlers, have priority over rendering, maintaining the integrity of the application state.

Figure 13.9. A page can be re-rendered between two macrotasks (mainline JavaScript execution and first click handler), while it can’t be rendered before a microtask is executed (before the promise handler). Figure 13.9. A page can be re-rendered between two macrotasks (mainline JavaScript execution and first click handler), while it can’t be rendered before a microtask is executed (before the promise handler).