event emitter js code example
Example 1: node js event emitter
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {
foo() {
this.emit('test');
}
}
const myEmitter = new MyEmitter();
myEmitter.on('test', () => console.log('Yay, it works!'));
myEmitter.foo();
Example 2: event module in node js
const myEmitter = new MyEmitter();
let m = 0;
myEmitter.on('event', () => {
console.log(++m);
});
myEmitter.emit('event');
myEmitter.emit('event');
Example 3: eventemitter nodejs
myEmitter.on('status', (code, msg)=> console.log(`Got ${code} and ${msg}`));