john33
john33

Reputation: 61

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.

Upvotes: 2

Views: 2657

Answers (1)

robertklep
robertklep

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

Related Questions