Pergunta clássica de Node.js em processo gringo. 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
- JS/Node.js is single-threaded → one Call Stack → one task at a time
- Async work is offloaded → does NOT block the Call Stack
- When async completes → the callback goes to the Task Queue (Macrotask)
- Promises → go to the Microtask Queue instead
- Event Loop flow:
- Is the Call Stack empty?
- YES → drain the ENTIRE Microtask Queue first
- then pick ONE task from the Task Queue
- after that task → drain the Microtask Queue again
- repeat...
- after that task → drain the Microtask Queue again
- then pick ONE task from the Task Queue
- YES → drain the ENTIRE Microtask Queue first
- Is the Call Stack empty?
Priority: Call Stack → Microtask Queue → Task Queue. Both queues are FIFO (first in, first out).
Goes to MICROtasks: Promises, queueMicrotask.
Goes to MACROtasks: setTimeout, setInterval, setImmediate, I/O callbacks.
The queues are fed by LibUV (Node.js) or the Web APIs (browser).
Ler é fácil. Falar isso em inglês, sob pressão, com alguém avaliando, é outra história. No VagaGringa esse tópico vira flashcard com repetição espaçada, cai em quiz e entra no simulado.
Treinar este tópico no app