const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.on('click', () => {
console.log('on click');
});
myEmitter.on('error', (err) => {
console.log(err);
});
myEmitter.emit('event');
myEmitter.emit('click');
myEmitter.emit('error',new Error("it is error"));
once
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.once('event', () => {
console.log('an event occurred!');
});
setInterval(()=>{
myEmitter.emit("event")
})