title: Why and when to use process.nextTick?
body: <p>Below is the code present in "practise01.js" file,</p>
  
  <pre><code>function fn(name){
     return f;
  
     function f(){
         var n = name;
         console.log("Next TICK "+n+", ");        
     }
  }
  
  function myTimeout(time,msg){
     setTimeout(function(){
         console.log("TIMEOUT "+msg);
     },time); 
  }
  
  process.nextTick(fn("ONE"));
  myTimeout(500,"AFTER-ONE");
  process.nextTick(fn("TWO"));
  myTimeout(500,"AFTER-TWO");
  process.nextTick(fn("THREE"));
  myTimeout(500,"AFTER-THREE");
  process.nextTick(fn("FOUR"));
  </code></pre>
  
  <p>The output from running above code is </p>
  
  <pre><code>rahul@rahul:~/myPractise/PlainNodeJSPractise01/Process$ node practise01.js                  
  
  Next TICK ONE, 
  Next TICK TWO, 
  Next TICK THREE, 
  Next TICK FOUR, 
  TIMEOUT AFTER-ONE
  TIMEOUT AFTER-TWO
  TIMEOUT AFTER-THREE
  </code></pre>
  
  <p>Now I wrote the code without using process.nextTick, in "practise02.js", as follows,</p>
  
  <pre><code>function myTimeout(time,msg){
    setTimeout(function(){
          console.log("TIMEOUT "+msg);
    },time);  
  }
  
  function fn(name){
    return f;
  
    function f(){
      var n = name;
      console.log("Next TICK "+n+", ");       
    }
  }
  
  fn("ONE")();
  myTimeout(500,"AFTER-ONE");
  fn("TWO")();
  myTimeout(500,"AFTER-TWO");
  fn("THREE")();
  myTimeout(500,"AFTER-THREE");
  fn("FOUR")();
  </code></pre>
  
  <p>after running the above code the output is </p>
  
  <pre><code>rahul@rahul:~/myPractise/PlainNodeJSPractise01/Process$ node practise02.js 
  Next TICK ONE, 
  Next TICK TWO, 
  Next TICK THREE, 
  Next TICK FOUR, 
  TIMEOUT AFTER-ONE
  TIMEOUT AFTER-TWO
  TIMEOUT AFTER-THREE
  </code></pre>
  
  <p>If you see both the outputs are same.</p>
  
  <p>So in which case I need to go with process.nextTick ?</p>
  
  <p>When I tried to read more, what I came to understand is If I need to execute some function immediately when the eventloop is empty than go for "process.nextTick".</p>
  
  <p>So how does its different from my second approach.</p>
  
  <p>Please explain me or give me some pointers</p>