简体   繁体   中英

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.

If you first emit and then listen, you're listening too late because the message has already been delivered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM