Node.js Course
Node.js
/
Advanced

Microtasks Queue

Definition

A special priority queue in Node.js that executes completely independently of the 6 main Event Loop phases. Microtasks run immediately after the currently executing script finishes, and between every single phase of the Event Loop.

Explain Like I'm New

Microtasks are the VIP pass at an amusement park. If the Event Loop is the normal line for the roller coaster, Microtasks get to skip the line. Whenever the main code finishes, Node immediately pauses everything and processes the entire Microtask queue before allowing the Event Loop to continue.

Real World Example

Resolved `Promises` (like `.then()` and `async/await`) are queued as Microtasks! This is why a resolved Promise will ALWAYS run before a `setTimeout`, even if the timeout is set to 0 milliseconds.

Common Use Cases

  • •Fine-tuning execution order
  • •Preventing UI or Server blocking

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What is the most common feature that uses the Microtask Queue?

intermediate

  • If a Promise resolves, does it run before or after `setTimeout`?

advanced

  • What happens if a Microtask queues another Microtask infinitely?

Flash Cards

Question

Most common feature?

Click to reveal answer
Answer

Promises! Any `.then()` or `catch()` callback is thrown straight into the Microtask Queue.

Question

Infinite Microtasks?

Click to reveal answer
Answer

It will completely crash/block the Node.js process. Because Node refuses to move to the next Event Loop phase until the Microtask Queue is completely empty, an infinite loop of Promises will starve the Event Loop, freezing your server.