Monday 8 October 2018

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

emitter.prependOnceListener(eventName, listener)
Prepend a one-time listener function for the event named eventName to the beginning of the listeners array.

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

var emitter = new EventEmitter();

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

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

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

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

console.log('\nEmitting "sayHello" event');
emitter.emit('sayHello');

console.log('\nEmitting "sayHello" event');
emitter.emit('sayHello');

Output
Emitting "sayHello" event
Hello Visitor

Prepended another listener function to the event "sayHello"

Emitting "sayHello" event
Good Morning!!!!!!!
Hello Visitor

Emitting "sayHello" event
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