EventEmitter class is used to raise and handle events. An objects emit events but all events are instances of the EventEmitter class. When object emits event ,event related functions are called synchronously.
Example
const EventEmitter = require('events');
class objEmitter extends EventEmitter {}
const myEventEmitter = new objEmitter();
myEventEmitter .on('event', () => {
console.log('an event fired!!');
});
myEventEmitter .emit('event');
Output :
an event fired!!
Example Explain :
The module 'events' included in a variable called 'EventEmitter' .Instance of 'EventEmitter' class created and assigned to 'myEventEmitter'.The
Below are some method of EventEmitter Class
one by one.
Example
const EventEmitter = require('events');
class objEmitter extends EventEmitter {}
const myEventEmitter = new objEmitter();
myEventEmitter .on('event', () => {
console.log('an event fired!!');
});
myEventEmitter .emit('event');
Output :
an event fired!!
Example Explain :
The module 'events' included in a variable called 'EventEmitter' .Instance of 'EventEmitter' class created and assigned to 'myEventEmitter'.The
eventEmitter.on()
method is used to register listeners and eventEmitter.emit() method is used to fires the event.Below are some method of EventEmitter Class
- emitter.addListener(eventName, listener)
- emitter.emit(eventName[, ...args])
one by one.
- emitter.eventNames()
- emitter.getMaxListeners()
- emitter.listenerCount(eventName)
- emitter.listeners(eventName)
- emitter.off(eventName, listener)
- emitter.on(eventName, listener)
- emitter.once(eventName, listener)
- emitter.prependListener(eventName, listener)
- emitter.prependOnceListener(eventName, listener)
- emitter.removeAllListeners([eventName])
- emitter.removeListener(eventName, listener)
- emitter.setMaxListeners(n)
No comments:
Post a Comment