const EventEmitter = require('events')
const myEmitter = new EventEmitter();
myEmitter.emit('newSale') //give name
//setup listener
myEmitter.on("newSale", ()=> {console.log("There was a new sale!")})
myEmitter.emit("newSale")
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
JS
copy
door.eventNames();
import { EventEmitter } from "node:events";
/**
* add "type":"module" in package.json
*/
class MyEmitter extends EventEmitter {}
const myEvent = new MyEmitter();
myEvent.on("event", () => {
console.log("an event occurred!");
});
myEvent.emit("event");
import { EventEmitter } from "node:events";
/**
* add "type":"module" in package.json
*/
class MyEmitter extends EventEmitter {}
const myEvent = new MyEmitter();
myEvent.on("event", () => {
console.log("an event occurred!");
});
myEvent.emit("event");