Node has a concept of event-driven programming, in which code is written to react rather than be called.
Node has an inbuilt event module about which you learn in-depth from here, we will use event listener and event emitter method of this class.
const EventEmitter = require('events');
const event = new EventEmitter();
Event Listener
An event listener is a logic which will be executed when an event is emitted. Make sure you have declared Event listener before emitting the event.
event.on('some-event', function() {
// some logic
});
//es6
event.on('some-event', () => {
// some logic
});
//with parameter
event.on('some-event', (parameter) => {
// some logic
});
Event Emitter
Event emitter is used to trigger a specific event listener.
event.emit('some-event', parameter);