Problem, as an interviewer would state it:
"Implement a Node-style EventEmitter class with on(event, fn), off(event, fn), once(event, fn), and emit(event, ...args)."
Examples:
const emitter = new EventEmitter();
emitter.on("data", (x) => console.log("got", x));
emitter.emit("data", 42); // "got 42"
Clarifying questions expected:
- If a listener calls
off()on itself (or another listener) WHILE emit is iterating, should that be handled safely? - Does
once()need to also support being manually removed viaoff()before it ever fires? - Should
emit()return a boolean indicating whether any listeners existed, matching Node's own real convention?
Code / implementation expected: Yes — real, direct proof of all 4 core methods, including the subtlest case: a self-removing listener executing mid-emit.