Question presented to candidate: "You register two listeners for the same custom event and emit it once. What order do they fire in, and what happens if you emit an 'error' event with no listener registered for it at all?"
What a strong answer should cover:
EventEmitteris Node's built-in publish/subscribe primitive:.on(event, listener)registers a callback,.emit(event, ...args)synchronously invokes every listener registered for that event, in registration order.- Multiple listeners on the same event all fire, in the order they were added — verified directly, not just documented, with two listeners producing a strictly ordered result.
.once(event, listener)registers a listener that fires at most one time, automatically removing itself after its first invocation — verified across two.emit()calls firing only once..emit()returns a boolean —trueif the event had at least one listener,falseif it had none — a real, checkable return value, not merely a side-effecting call.- 📌 The critical, special-cased behavior: emitting
'error'on anEventEmitterwith no listener registered for it throws synchronously rather than silently doing nothing — this is a deliberate design choice specifically for the'error'event name, different from every other event, and is why every stream/socket/emitter-based API in Node's ecosystem needs an'error'listener attached. EventEmitteris the foundation underneath Streams,net.Socket,http.Server, and much of Node's standard library — understanding it deepens understanding of all of those, not just custom application-level events.
Clarifying questions expected:
- "Is the concern custom application events, or understanding a built-in class (a stream, a socket) that happens to extend EventEmitter?" — the mechanics are identical either way.
- "Does the codebase register an
'error'listener on every emitter that might emit one?" — the special-cased throw-on-no-listener behavior makes this a real, checkable requirement, not just good practice.
Code / implementation expected: Yes — actually demonstrating listener order, .once(), the boolean return value, and the special 'error'-with-no-listener throw is the concrete, convincing version of this answer.