Loop Quest
The event loop: call stack, timers, promises, async/await, nextTick, setImmediate, events and errors.
World 1 One chef, one stove
Node is a kitchen with exactly one chef. The call stack is what the chef is cooking right now, and the chef finishes it before touching anything else.
World 2 Alarm clocks
setTimeout is an alarm clock. It never interrupts the chef. When it rings, its callback waits in line until the stove is free.
World 3 The express lane
Promise callbacks are the express lane. Whenever the chef's hands are free, the express lane is emptied before any alarm clock gets a turn.
- 11Promises beat timers
- 12new Promise runs now
- 13Chains take turns
- 14await pauses one function
- 15Errors skip to .catch
- 16Promise.all keeps order
- 17async always returns a promise
- 18A throw becomes a rejection
- 19finally passes it on
- 20Awaiting a timer
- 21Promise.race takes the first
- 22allSettled waits for everyone
World 4 The VIP line
process.nextTick is the VIP line. It cuts in front of even the express lane, so use it sparingly.
World 5 Round the loop
The event loop walks a circle: timers, then I/O (deliveries), then check (setImmediate), and back again. Each stop has its own queue.
World 6 Events and errors
An EventEmitter is a doorbell wired straight to the kitchen: pressing it makes the chef answer right now. Errors only reach a catch that is still on the stack.
World 7 Don't block the chef
With one chef, a dish that takes ten minutes stops every other order. Long synchronous work, or an endless VIP line, freezes the whole server.
Your rulebook
Finish a level to add its rule. Fill the book and you can explain all of this in an interview.
- 1Finish Top to bottom to unlock this rule.
- 2Finish Functions stack up to unlock this rule.
- 3Finish Last in, first out to unlock this rule.
- 4Finish A throw unwinds the stack to unlock this rule.
- 5Finish 0ms still waits to unlock this rule.
- 6Finish Shortest alarm first to unlock this rule.
- 7Finish A timer inside a timer to unlock this rule.
- 8Finish Equal alarms to unlock this rule.
- 9Finish The clock starts now to unlock this rule.
- 10Finish An alarm that repeats to unlock this rule.
- 11Finish Promises beat timers to unlock this rule.
- 12Finish new Promise runs now to unlock this rule.
- 13Finish Chains take turns to unlock this rule.
- 14Finish await pauses one function to unlock this rule.
- 15Finish Errors skip to .catch to unlock this rule.
- 16Finish Promise.all keeps order to unlock this rule.
- 17Finish async always returns a promise to unlock this rule.
- 18Finish A throw becomes a rejection to unlock this rule.
- 19Finish finally passes it on to unlock this rule.
- 20Finish Awaiting a timer to unlock this rule.
- 21Finish Promise.race takes the first to unlock this rule.
- 22Finish allSettled waits for everyone to unlock this rule.
- 23Finish nextTick cuts in to unlock this rule.
- 24Finish A tick inside a promise to unlock this rule.
- 25Finish The full lineup to unlock this rule.
- 26Finish Ticks first, as a group to unlock this rule.
- 27Finish A tick inside a tick to unlock this rule.
- 28Finish Between two timers to unlock this rule.
- 29Finish Timeout or immediate? to unlock this rule.
- 30Finish Inside I/O, immediate wins to unlock this rule.
- 31Finish An immediate inside an immediate to unlock this rule.
- 32Finish Inside I/O: the full lineup to unlock this rule.
- 33Finish emit is synchronous to unlock this rule.
- 34Finish once fires once to unlock this rule.
- 35Finish try/catch misses the future to unlock this rule.
- 36Finish await makes try/catch work to unlock this rule.
- 37Finish Cutting in line to unlock this rule.
- 38Finish The 'error' event to unlock this rule.
- 39Finish emit doesn't wait to unlock this rule.
- 40Finish Nobody caught it to unlock this rule.
- 41Finish A busy stack delays timers to unlock this rule.
- 42Finish An endless VIP line to unlock this rule.
- 43Finish An endless express lane to unlock this rule.
- 44Finish Cut the work into chunks to unlock this rule.