Reputation: 61
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.
Upvotes: 2
Views: 2657
Reputation: 203231
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.
Upvotes: 3