Microtasks and nextTick
Local practice: Node.js 22.16.0 on Linux x64; proposed teaching baseline 26.13.1 remains untested. Copy only the listed files into a fresh disposable directory. No dependency installation or external services required. These are static Node.js examples, not verified ByteMind runner tasks.
Top-level CommonJS ordering
console.log('start');process.nextTick(() => console.log('tick'));Promise.resolve().then(() => console.log('promise'));queueMicrotask(() => console.log('microtask'));console.log('end');Run:
node main.cjsExpected stdout:
startendtickpromisemicrotaskExit status: 0. Expected stderr: empty.
How it works
At a typical CommonJS top-level boundary, the next-tick queue drains before promise microtasks. ESM evaluation already runs in an asynchronous module evaluation context, so the top-level observation differs: the queued promise and microtask in the example run before nextTick. Within one microtask queue, enqueue order determines the order of the two callbacks in this fixture. Prefer queueMicrotask for portable ordinary deferred work; Node documents nextTick as legacy in the local version. Neither should be used for unbounded recursive scheduling because I/O may be starved.
Top-level ES module ordering
console.log('start');process.nextTick(() => console.log('tick'));Promise.resolve().then(() => console.log('promise'));queueMicrotask(() => console.log('microtask'));console.log('end');Run:
node main.mjsExpected stdout:
startendpromisemicrotasktickExit status: 0. Expected stderr: empty.
Common mistake
Do not memorize a single order that ignores module format and the point where callbacks are queued. Changing to ESM can change the observed ordering without changing the source statements.
Practice
Run the same two snippets in separate files as shown. Swap the promise and queueMicrotask lines and predict their relative order, then observe it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Microtasks and nextTick — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which queue is shared by promise reactions and queueMicrotask?
Why are the two fixture outputs different?
What can recursively scheduling microtasks without a bound do?