macrotask
An ordinary task scheduled on the event loop, such as a timer or I/O callback, as opposed to a microtask.
In more detail
Only one macrotask runs at a time, and all pending microtasks run after it before the next one starts. Specifications usually just call them tasks. In a browser the page can also redraw between tasks, so splitting long work into several tasks, for example with setTimeout(fn, 0), keeps it responsive.
Related terms
- 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.
- 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.
- setTimeout — A JavaScript function that runs a callback once after at least the given number of milliseconds, unless clearTimeout cancels it first.
- long task — A task that keeps the main thread busy for more than 50 milliseconds, long enough for users to notice lag.
- rendering opportunity — A point in the event loop, between tasks, when the browser may run animation callbacks, recalculate style and layout, and paint a new frame.
Taught in
- The Browser Event Loop: Call Stack, Microtasks And Macrotasks — Frontend Architecture & Web Platform
- requestAnimationFrame vs requestIdleCallback: High-FPS Work — Frontend Architecture & Web Platform
- Microtask Queues: process.nextTick vs Promise Timing — Node.js Internals & Backend Services