Sunday, 7 October 2018

node.js: emitter.prependListener(eventName, listener)

emitter.prependListener(eventName, listener)
Prepend a listener function to the beginning of the array for the event named eventName.

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

var emitter = new EventEmitter();

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

emitter.emit('sayHello');

console.log('Prepended another listener function to the event "sayHello"');

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

emitter.emit('sayHello');


Output
Hello Visitor
Prepended another listener function to the event "sayHello"
Good Morning!!!!!!!
Hello Visitor

Note
a.   This method returns a reference to the EventEmitter, so that calls can be chained.


Previous                                                 Next                                                 Home

No comments:

Post a Comment