Courses / Node.js: From First Script to Production / Chapter 26
Module 10 · Case study and drills · Chapter 26 of 27
Predict the Output: Timed Event Loop and Async Drills
Builds on Asynchronous Programming: Callbacks, Promises and async/await, Event-Driven Programming with EventEmitter, Core Modules: Files, Paths, Buffers, Crypto and util, Inside the Event Loop: Phases, Timers and setImmediate and process.nextTick, Promise Jobs and the Microtask Queue. Next up: Mock Interview: Five Timed Node.js Prompts with Model Answers.
Part 1 · Set 1: warm-up (10 minutes)
How to use these sets
Each set is a timed round of predict-the-output puzzles. Cover the answer, write the exact printed lines on paper, and only then open the answer. A guess that is nearly right does not count: the order of every line is the point.
Every answer here comes from a real run on Node 24.18.0, on Windows and again on Linux in WSL, and the two printed the same lines. Each puzzle was also run 50 times on each system and printed the same order every time. That matters, because some orders in Node are not fixed and a drill on them would teach nothing.
Two orders vary: setTimeout(fn, 0) against setImmediate in the main module, and the order of a socket close event against a timer on Windows. The event loop chapter shows both varying. No puzzle depends on either.
The four rules below cover most of the first set. The microtasks chapter and the event loop chapter explain why they hold, and each answer here points back to one of them.