Thursday 4 October 2018

node.js: emitter.emit(eventName[, ...args])

This function calls all the listeners registered for this event synchronously.

Below table summarizes the arguments of emit function.
Argument
Type
Description
eventName
String
Specify the event name.
args
Any
Arguments passed to listener function

This method returns true if the event had listeners, false otherwise.

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

var emitter = new EventEmitter();

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

emitter.on("welcome", (firstName, lastName) => {
 console.log("Good Morning Mr.%s %s", firstName, lastName);
});

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


Output
Hello Mr.krishna
Good Morning Mr.krishna Ponnam



Previous                                                 Next                                                 Home

No comments:

Post a Comment