Monday 8 October 2018

node.js: emitter.removeAllListeners([eventName])

emitter.removeAllListeners([eventName])
Remove all listeners associated with this event.

HelloWorld.js
var EventEmitter = require('events').EventEmitter;

var emitter = new EventEmitter();

emitter.on('sayHello', () => {
 console.log('Hello World');
});

emitter.on('sayHello', () => {
 console.log('Good Morning');
});

console.log('Emitting "sayHello" event');

emitter.emit('sayHello');

console.log('Removing all listeners for "sayHello" event');
emitter.removeAllListeners('sayHello');

console.log('Emitting "sayHello" event');


Output

Emitting "sayHello" event
Hello World
Good Morning
Removing all listeners for "sayHello" event
Emitting "sayHello" event


Previous                                                 Next                                                 Home

No comments:

Post a Comment