NodeJs
Event Emitter
What is EventEmitter?
In Nodejs there is a special system called EventEmitter. It helps your program listen for something to happen and do something when it happens.
- One part emits (sends) an event
- Another part listens and reacts when that event happens
Why it is Used
Node.js is built on event-driven architecture. That means it reacts to events all the time.
Examples of events:
- When a file finishes reading
- When a user clicks something
- When data comes from a network
EventEmitter is the core of how Node.js handles many things.
Interview Answer
EventEmitter is a class in Node.js that helps create and handle custom events.
Key points:
- It works on the publish and subscribe model
- One part emits an event
- Other parts subscribe or listen to that event
- You use
onto listen andemitto trigger - It helps write clean and modular code