Oh My AlgorithmKO
←
JavaScript Runtime
← All catalogs

JavaScript Runtime

Node.js callbacks, microtasks, and the event loop

Node.js Event Loop

Principles and uses

After synchronous code, process ready work in the defined order.

Use Async ordering and delays

Schedule four operations inside an I/O callback. Scheduling does not execute them immediately; current synchronous code finishes first.

Synchronous codeRunningPrioritynextTickQueuedFollow-upPromiseQueuedImmediatesetImmediateQueuedTimersetTimeoutQueuedNode.js ≥20 · CommonJS I/O
↔ Drag to explore
fs.readFile('data.txt', () => { /* schedule callbacks */ });
Learn more

The example schedules nextTick, Promise, setImmediate, and setTimeout(0) inside a CommonJS I/O callback on Node.js 20+. Under these conditions the order is nextTick, Promise, immediate, timer; do not generalize the top-level immediate/timer order. Timers run after poll since the libuv change in Node 20. Long synchronous work delays other callbacks.

Learning topics