Skip to content

Microtask

In one line

A microtask is a short function that runs after the current task completes but before the next macrotask — primarily Promise callbacks.

In simple words

Microtasks are small functions queued by Promise resolutions (and queueMicrotask()) that run after the current synchronous code finishes but before any macrotask (like setTimeout). The event loop drains the entire microtask queue before moving to the next macrotask.

This means all pending Promise callbacks run before the next setTimeout callback, even if the setTimeout was registered first. This is why Promise.resolve().then(() => ...) runs before setTimeout(() => ..., 0).

Understanding microtasks vs macrotasks is important for predicting the order of async code execution and avoiding subtle bugs in complex async flows.

Example

javascript
setTimeout(() => console.log("macrotask"), 0);
Promise.resolve().then(() => console.log("microtask"));
console.log("sync");
// Output: sync, microtask, macrotask

Microtasks (Promise) run before macrotasks (setTimeout).

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary