Event Emitters in Node.js
Event Emitters in Node.js
Much of the Node.js core API is built around an asynchronous event-driven architecture in which certain kinds of objects (called "emitters") emit named events that cause Function objects ("listeners") to be called. All objects that emit events are instances of Event Emitter class. These objects expose an event Emitter On () function that allows one or more functions to be attached to named events emitted by the object. When the Event Emitter objects emit an event, all of the functions attached to that specific event are called synchronously.
Event Emitter Class: Event Emitter lies in the events module. It is accessible through the code.
When an Event Emitter instances face any error, it emits an ‘error’ event. When a new listener is added, ‘newListner’ event is fired and when a listener is removed, ‘remove Listener’ event is fired. Event Emitter provides multiple properties like on and emit. on a property is used to bind a function with the event and emit is used to fire an event.
Event Emitter Methods
Events:
Example
Run the above code.