title: Event Loop and EventEmitter in Node JS
body: <p>I have written some basic Nodejs code in which has</p>
  
  <ol>
  <li>a recursive function <code>myFunction</code> with <code>process.nextTick()</code></li>
  <li>a long running for loop</li>
  </ol>
  
  <p>Both blocks are emitting events, The code is as follows</p>
  
  <pre><code>var EventEmitter = require('events').EventEmitter;
  var emitter = new EventEmitter();
  var counter = 1;
  
  emitter.on('message',
      (message)=&gt;
          {
              console.log('Hey I have got this message ' +message);
          }
      );
  
  emitter.on('syncMsg',
      (message)=&gt;
          {
              console.log('voila the sync message is  ' +message);
          }
      );
  
  function myFunction(){
      process.nextTick(
      ()=&gt;
      {
          emitter.emit('message','message'+counter);
          counter++;
          myFunction();
      }
      )
  }
  
  myFunction();
  
  for(var i=0;i&lt;10000;i++)
  {
      emitter.emit('syncMsg','syncMsg'+counter);
      counter++;
  }
  </code></pre>
  
  <p>Now while executing the file, the listener for <code>syncMsg</code> event gets triggered synchronously before even a single event being emitted for <code>message</code> event. Why is the execution behaving synchronously?</p>