0
var events = require('events');                                                                                                                                                            
var eventEmitter = new events.EventEmitter();                                                                                                                                              

var ringBell = function ringBell()                                                                                                                                                         
{                                                                                                                                                                                          
 console.log('ring ring ring');                                                                                                                                                            
}                                                                                                                                                                                          

var lockDoor = function lockDoor()                                                                                                                                                         
{                                                                                                                                                                                          
console.log("lock door");                                                                                                                                                                  
}                                                                                                                                                                                          

eventEmitter.on("ringBell", ringBell);                                                                                                                                                     
eventEmitter.on("lockDoor", lockDoor);                                                                                                                                                     
eventEmitter.emit('ringBell');                                                                                                                                                             
eventEmitter.emit('lockDoor');

So the ringBell event is emitted first followed by the lockDoor Event.

My question is that does ringBell event handler complete first before the lockDoor event is emitted ?

theDarkerHorse
  • 109
  • 1
  • 1
  • 15

1 Answers1

0

In this case yes, but if you were to put something more complex in your event handlers, like an asynchronous db call, then it would start the lockDoor event handler before completing the ringBell event handler.

Logan Tegman
  • 829
  • 5
  • 9
  • "before completing the ringBell event handler" --- that's wrong. You cannot run the half of an event handler. All `ringBell` handlers are called completely before any `lockDoor` handler. It's guaranteed to be so. – zerkms May 04 '15 at 02:43
  • If the first event handler contains an asynchronous function it won't block execution of the other event handlers. Try putting something like a db read with a promise or a timeout in his `ringBell` event handler (and hold the `console.log` until this completes) and you'll find the that the `lockDoor` `console.log` call is run before the asynchronous code completes and the `ringBell` message is printed. – Logan Tegman May 04 '15 at 02:56
  • It will not be an event emitter handler then. Event handlers are always evaluated **COMPLETELY**. Put the code that demonstrates what you say and I will show you where your statement becomes falsy. – zerkms May 04 '15 at 03:01
  • Most likely will the be called in order but that does necessarily mean they also complete in order - depending on the actual code in `ringBell`. – J.G.Sebring May 04 '15 at 19:42