Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created October 15, 2018 11:19
Show Gist options
  • Save salmanx/50b31ec21cde6fcceab5a993875a82cd to your computer and use it in GitHub Desktop.
Save salmanx/50b31ec21cde6fcceab5a993875a82cd to your computer and use it in GitHub Desktop.
simple code example how EventEmitter() works in node js
// 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