CodePark/ questions / EventEmitter
START PRACTICINGPRACTICE
Backend question · Node.js › EventEmitter

An EventEmitter emits an error event and no error listener is registered. What happens?

The error is thrown from emit; if nothing catches it, the process crashes

The error is thrown from emit. If nothing catches it, the process crashes.

Node treats the
error event in a special way. If no listener exists, emit('error', err) throws err. An uncaught throw stops the whole process.

"The event is ignored and
emit returns false" is wrong for error. That rule is true only for other events: an event with no listeners is silently ignored, and emit returns false.

This is why you should always add
error handlers to streams, sockets and servers.