Loading...
Loading...
queueMicrotask() is a built-in JavaScript function that adds a task to the microtask queue.
Microtasks execute immediately after the current call stack completes and before the next macrotask (e.g., setTimeout, setInterval, event handler).
queueMicrotask(() => {
// Your code
});
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
queueMicrotask(() => {
console.log("Microtask Hack Frontend");
});
console.log("End");
Start
End
Microtask Hack Frontend
Timeout
queueMicrotask always executes before setTimeout, even if the timer delay is 0.
Tip:
Use queueMicrotask if you want to execute a task asynchronously but immediately after the current operation — faster than setTimeout(...).
queueMicrotask() is a way to add a task to microtasks that will execute after the current stack but before macrotasks.