Typorithm · hard

nextTick & Microtasks

node-nexttick-microtasksnodejsnext-tickmicrotaskspromises
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn nextTick & Microtasks

0%

Event Loop

process.nextTick callbacks run before the Promise microtask queue, and both queues drain completely between every event-loop phase (and right after the current operation finishes). Because nextTick runs ahead of everything, recursively scheduling it can starve the loop and never let I/O or timers run.

process.nextTick callbacks have the highest priority: they run before the Promise microtask queue, so a nextTick scheduled alongside a resolved promise always logs first.

Both the nextTick queue and the microtask queue are fully drained after the current operation completes and again between every event-loop phase — they are not tied to a single phase the way timers or setImmediate are.

Because nextTick jumps ahead of I/O and timers, recursively calling process.nextTick can starve the loop: the queue never empties, so the loop never advances to its phases and pending I/O never runs.

01

process.nextTick runs before the Promise microtask queue.

02

Both queues drain between every event-loop phase.

03

Recursive nextTick can starve the loop — I/O never runs.

// nextTick callbacks run BEFORE the Promise microtask queue
process.nextTick(() => console.log('1: nextTick'));
Promise.resolve().then(() => console.log('2: promise'));
setTimeout(() => console.log('3: timeout'), 0);
// output: 1: nextTick, 2: promise, 3: timeout
// nextTick + microtasks both drain between every event-loop phase

Read the implementation

Connect each part of the code to the concept before you type.

1. nextTick first

process.nextTick callbacks run before the Promise microtask queue, so this logs first.

process.nextTick(() => console.log('1: nextTick'));

2. Promise microtask

Promise .then callbacks are microtasks, drained after the nextTick queue but before any timer.

Promise.resolve().then(() => console.log('2: promise'));

3. Resulting order

nextTick beats the promise microtask, and both run before the timers-phase setTimeout(0).

// output: 1: nextTick, 2: promise, 3: timeout

4. Drain between phases

Both the nextTick queue and the microtask queue are emptied between every event-loop phase and after the current operation.

// nextTick + microtasks both drain between every event-loop phase

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

nextTick & Microtasks: Lesson & Practice · Typorithm | ApplyDjinn