Thursday, 4 October 2018

emitter.addListener(eventName, listener): Add listener to this event

emitter.addListener(eventName, listener)
This function add listener to this event.

Below table summarizes the arguments of addListener function.
Argument
Type
Description
eventName
String
Event name
Listener
Function
Callback function to be called on this event emit

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

var emitter = new EventEmitter();

emitter.addListener("welcome", (name) => {
 console.log("Hello Mr.%s", name);
});

emitter.addListener("welcome", (name) => {
 console.log("Good Morning Mr.%s", name);
});

emitter.emit("welcome", "krishna");


Output

Hello Mr.krishna
Good Morning Mr.krishna


Previous                                                 Next                                                 Home

No comments:

Post a Comment