你如何在node.js中定义侦听器?
它们是侦听某些事件发生的事件发射器对象,然后通知事件发射器或调用回调函数?
感谢任何帮助
答案 0 :(得分:0)
这是一个简单的例子:
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('someCoolEvent', () => { // Here is where we are listening for event to occur
console.log('an event occurred!');
});
myEmitter.emit('someCoolEvent'); // here is how we emit the event