Node.js侦听器定义

时间:2017-06-02 19:30:25

标签: node.js listener

你如何在node.js中定义侦听器?

它们是侦听某些事件发生的事件发射器对象,然后通知事件发射器或调用回调函数?

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

使用Event Emitter

这是一个简单的例子:

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