[英]Node JS Event emitter not working if we first emit and then we listen
var events=require('events').EventEmitter;
var eventEmitter = new events();
eventEmitter.on('hello', function()
{
console.log('Hey !!');
});
eventEmitter.emit('hello');
now if i first emit and then listen it will not print console.Help me to find the reason. 现在,如果我先发出然后听,它将不会打印控制台。请帮助我查找原因。
EventEmitter
delivers events synchronously, which means that when you emit a message, that message is delivered to any listeners right away. EventEmitter
同步传递事件,这意味着当您发出消息时,该消息会立即传递给所有侦听器。
If you first emit and then listen, you're listening too late because the message has already been delivered. 如果您先发出然后听,那您听得太迟了,因为该消息已经传递完毕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.