You now have two ways to hear about something later. readFile() called one function back, a callback, when the house rota had loaded, and the Washer class emitted "done" to every listener that had signed up. Which one should an API offer?
In this lesson, you’ll meet Node.js’s second kind of emitter, EventTarget, and a rule for choosing between a callback and an event. Start with the emitter that comes from the web standards.
Listen With EventTarget
EventEmitter is Node.js’s own class. Web browsers have a different one, EventTarget. Node.js includes it too, as a global, so you don’t import it. You’ll meet objects built on it in a later part of the path, so its method names are worth knowing now.
It works like EventEmitter, with longer method names. You add a listener with .addEventListener(), and you announce an event with .dispatchEvent(), which takes an Event object made from the event’s name.
Passing { once: true } as the third argument to .addEventListener() does what .once() did for the washer. Here’s dryer_target.mjs, where Wren wants to hear about one load only: