Created
October 15, 2018 11:19
-
-
Save salmanx/50b31ec21cde6fcceab5a993875a82cd to your computer and use it in GitHub Desktop.
simple code example how EventEmitter() works in node js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Basic events functionality | |
// const EventEmitter = require('events'); | |
// const emitter = new EventEmitter(); | |
// emitter.on('messageLogged', function(args){ | |
// console.log(`${args.name} : ${args.age}`) | |
// console.log("Listener called"); | |
// }); | |
// emitter.on('customEvent', function(status, message){ | |
// console.log(`${message} : ${status}`); | |
// }) | |
// emitter.emit('messageLogged', { name: "salman", age: "27" }); | |
// emitter.emit('customEvent', 200, "Hellow new world!"); | |
const EventEmitter = require('events').EventEmitter; | |
const util = require("util"); | |
var Person = function(name){ | |
this.name = name; | |
} | |
util.inherits(Person, EventEmitter); | |
var p = new Person('Salman'); | |
p.on('speak', function(said){ | |
console.log(`${this.name} is saying ${said}`); | |
}); | |
p.emit('speak', "Hellow new world!"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment