EventEmitter
EventEmitter lets objects emit named events and listeners react.
Example
const EventEmitter = require("events");
const e = new EventEmitter();
e.on("hi", (name) => console.log("hello", name));
e.once("hi", () => console.log("only first"));
e.emit("hi", "Ada");
e.emit("hi", "Kofi");
console.log(e.listenerCount("hi"));Result
hello Ada only first hello Kofi 1
Exercise
Fire an event.
e.("hi")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.