Node
Q & A
Event Driven

Event Emiiter

InterviewBit

1.What is an Event Emitter in Node.js?

EventEmitter is a Node.js class that includes all the objects that are basically capable of emitting events. This can be done by attaching named events that are emitted by the object using an eventEmitter.on() function. Thus whenever this object throws an even the attached functions are invoked synchronously.

const { EventEmitter } = require("events");
const MyEventEmitter = new EventEmitter();
 
MyEventEmitter.on("send", (cb) => {
  console.log("Event Listner"); // Event Listner
  cb("Event Emitter");
});
 
MyEventEmitter.emit("send", (msg) => {
  console.log("Event Emitter"); // Event Emitter
  console.log(msg); // Event Emitter
});