Guia de entrevista · Node.js

What is the difference between process.nextTick(), setImmediate(), and setTimeout(fn, 0)?

do caderno de estudos · atualizado em 2 de outubro de 2026
Para quem vai encarar entrevista de Node.js2 min

Não decore palavra por palavra: entenda a estrutura da resposta e refaça com as suas palavras, em inglês.

A resposta, como você fala na entrevista

process.nextTick(): schedules work after the current operation, before the event loop proceeds. Node maintains a separate queue for it.
setImmediate(): schedules a callback in the check phase after poll.
setTimeout(fn, 0): schedules a timer with a minimum threshold, not a promise to run immediately or at an exact time.

In ordinary CommonJS synchronous code, nextTick callbacks run before queued Promise microtasks. ES module top-level evaluation and await continuations already run in a microtask context, so newly queued Promise callbacks can run before nextTick work. Do not apply one universal priority diagram to every context.

Trap: when both are scheduled from the main module, the order of a zero-delay timer and setImmediate is not guaranteed. When both are scheduled inside the same I/O callback, setImmediate runs first. Recursive nextTick or microtask work can starve normal loop progress.

Source: Node.js scheduling examples.

Pegadinha clássica

Essa pergunta tem uma armadilha que derruba muita gente em entrevista. Ela está marcada na resposta acima: vale reler antes de treinar.

Ler é o primeiro passo. No app, essa pergunta vira flashcard com repetição espaçada, e o módulo tem quiz e simulado para testar o que ficou.

Treinar essa pergunta no app