How Node really works · Chapter 12 of 34
Events
By the end of this chapter you can let one piece of code announce what happened and any number of others react to it, and wait for the one event you care about. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Events and streams
- 5 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
Every order at Tapri has to reach the stove screen and the kitchen printer, and Ravi wants the rider board to hear about it too. On Monday the opening routine (lights, boiler, the first pot of chai) ran twice, because the kitchen said "ready" twice. When the milk ran out, the error took the whole order server down. And Anjali's tracking page has to wait for one particular order, not every order.
With the Node you know so far, the order code would call the stove, the printer and the rider board by name. Every new screen means editing it, and the order code has to know every screen. Tapri needs it to say "new order" once, and let whoever cares listen.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
KitchenFeed: announce every new order
Write the class KitchenFeed, which extends EventEmitter. Its method placeOrder(order) emits 'order' with the order as the only argument, so every listener gets the very same object, and returns how many orders this feed has placed so far, counting this one (1 for the first call, 2 for the second).
Each feed keeps its own count: a new feed starts again at 1.
- Task 2
onFirstReady: run the opening routine only once
Write onFirstReady(emitter, openKitchen). It makes openKitchen run the first time emitter emits 'ready', and never again, however many more times ready is emitted.
After that first ready, no listener of yours may be left on the emitter: its listener count for 'ready' must be 0. It returns nothing.
- Task 3
KitchenFeed: keep the feed alive when an order goes wrong
Give every KitchenFeed an errors array, empty at the start. Emitting 'error' on a feed must never throw: each error emitted is pushed to feed.errors, the error object itself (not its message), in the order they came.
- Task 4
waitForOrder: wait for one particular order, then stop listening
Write waitForOrder(feed, id). It returns a promise straight away, and from that moment exactly one listener of yours is on the feed's 'order' event. Orders with another id are ignored, and the listener stays. When an order with that id is emitted, the promise resolves with that very order object and your listener is removed, so the count for 'order' goes back to what it was.
Remove only your own listener: other code may be listening to the same feed.
- Task 5 · interview question
Interview: predict when the listeners run
The starter shows a snippet that adds listeners to a bell, emits ring twice, and schedules a setImmediate, pushing to log as it goes. Set predictedLog to the final contents of log, once the immediate has run: seven entries, the same strings, in order.
The check runs the same snippet and compares the two lists in order.
What you will learn
- EventEmitter: on to listen, emit to announce, and classes that extend it.
- once and off: a listener that runs one time, and removing one you added.
- The error event: why emitting it with nobody listening crashes the process.
- Waiting for an event: turning "when this happens" into a promise.
More in Events and streams
Build "Events" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.