title: Javascript Promises explicit functions vs. inline functions
body: <p>I'm trying to understand the whacky world of javascript promises and came across this that I don't understand.  </p>
  
  <p>The first program is from a book explaining promise chaining and works just as you would think:</p>
  
  <pre><code>var Promise = require('promise');
  
  function delay(time) {
      return new Promise( function(resolve,reject) {
          setTimeout( resolve, time ); 
      });
  }
  
  delay(1000) // step 1
      .then(function STEP2(){
          console.log( "step 2b (after 1000ms)" );
          return delay( 2000 );
      })
      .then(function STEP3(){
          console.log( "step 3b (after another 2000ms)" );
      })
      .then(function STEP4(){
          console.log( "step 4b (next Job)" );
          return delay( 5000 );
      })
      .then(function STEP5() {
          console.log( "step 5b (after another 5000ms)" );
      });
  </code></pre>
  
  <p>The console logs appear after the correct amount of delays.</p>
  
  <p>NOW, in order to make this clearer in my own mind, I made the STEP functions explicitly so the program now looks like this:</p>
  
  <pre><code>var Promise = require('promise');
  
  function delay(time) {
      return new Promise( function(resolve,reject){
          setTimeout( resolve, time );
      });
  }
  function STEP2() {
      console.log( "step 2 (after 1000ms)" );
      return delay( 2000 );
  }
  function STEP3() {
      console.log( "step 3 (after another 2000ms)" );    
  }
  function STEP4() {
      console.log( "step 4 (next Job)" );
      return delay( 5000 );
  }
  function STEP5() {
      console.log( "step 5 (after another 5000ms)" );
  }
  
  delay( 1000 ).then(STEP2()).then(STEP3()).then(STEP4()).then(STEP5());
  </code></pre>
  
  <p>But now all the console logs happen at once, the program delays 5000 ms and then exits.  Can someone explain what is different (functionally) between the two examples above?  Thank you.</p>