microtask
A small job, such as a promise callback, that runs as soon as the current piece of JavaScript finishes, before the event loop handles anything else.
In more detail
The whole microtask queue is emptied each time the call stack becomes empty, so microtasks that keep queuing more can block everything else. Promise callbacks and queueMicrotask() create microtasks.
Examples
In the browser: The event loop drains the whole microtask queue after every task, including microtasks added while draining. That makes microtasks fast but risky: a chain that keeps adding more starves everything else, including rendering in a browser.In Node.js, process.nextTick() callbacks run even before promise microtasks.The course exercise uses them to simulate interleaving urgent and background work.Related terms
- setTimeout — A JavaScript function that runs a callback once after at least the given number of milliseconds, unless clearTimeout cancels it first.
- macrotask — An ordinary task scheduled on the event loop, such as a timer or I/O callback, as opposed to a microtask.
- event loop — The central loop of an asynchronous program that waits for events, such as timers firing or data arriving, and runs the matching callbacks.
- Promise — A JavaScript object that stands for a result that is not ready yet and will later either succeed with a value or fail with an error.
- queueMicrotask() — A standard function that schedules a callback as a microtask, the same queue promise callbacks use.
- starvation — When one kind of work keeps getting the processor so other work, such as rendering, never gets a turn.
- process.nextTick() — A Node.js function that queues a callback to run as soon as the current operation finishes, before promise callbacks and before the event loop continues.
- main thread — The first thread a program starts with; in a browser page it also runs JavaScript and handles input, layout and painting.
Taught in
- Overlay Architecture: Portals, Stacking Contexts & Backdrops — Design Systems & Component Engineering
- Layout, Reflow, Paint And GPU Compositing Layers — Frontend Architecture & Web Platform
- MutationObserver: Reactive DOM Watching Without Polling — Frontend Architecture & Web Platform
- The Browser Event Loop: Call Stack, Microtasks And Macrotasks — Frontend Architecture & Web Platform
- Event Loop Lifecycle: The Six Phases & libuv uv_run — Node.js Internals & Backend Services
- Microtask Queues: process.nextTick vs Promise Timing — Node.js Internals & Backend Services
- Routing & Middleware: Path Parsing & Async Context — Node.js Internals & Backend Services
- Concurrent Mode: Interruptible Work And Lane Priority — React 19 & Modern Component Architecture