title: Nested while loop for promise
body: <p>I have followed the post <a href="https://stackoverflow.com/questions/24660096/correct-way-to-write-loops-for-promise">Correct way to write loops for promise.</a> to create loops for promise successfully. 
  <p>However, it seems that this method doesn't work with nested loop</p>
  
  <p>The loop I want to simulate:</p>
  
  <pre><code>var c = 0;
  while(c &lt; 6) {
      console.log(c);
      var d = 100;
      while(d &gt; 95) {
          console.log(d);
          d--;
      } 
      c++;
  }
  </code></pre>
  
  <p>Promised <strong>(note that I simplified the logic of promFunc() here, so do not assume it is useless)</strong> :</p>
  
  <pre><code>var Promise = require('bluebird');
  var promiseWhile = Promise.method(function(condition, action) {
      if (!condition()) return;
          return action().then(promiseWhile.bind(null, condition, action));
      }); 
  
  var promFunc = function() {
      return new Promise(function(resolve, reject) {
          resolve(); 
      }); 
  };
  
  var c = 0;
  promiseWhile(function() {
      return c &lt; 6;
  }, function() {
      return promFunc()
          .then(function() {
              console.log(c);
  
              // nested
              var d = 100;
              promiseWhile(function() {
                  return d &gt; 95; 
              }, function() {
                  return promFunc()
                      .then(function() {
                          console.log(d);
                          d--;
                      }); 
              })// .then(function(){c++}); I put increment here as well but no dice...
  
              c++;
          }); 
  }).then(function() {
      console.log('done');   
  });
  </code></pre>
  
  <p>Actual result:</p>
  
  <pre><code>0
  100
  1
  99
  100
  2
  98
  99
  100
  3
  97
  98
  99
  100
  4
  96
  97
  98
  99
  100
  5
  96
  97
  98
  99
  100
  96
  97
  98
  99
  96
  97
  98
  96
  97
  done
  96
  </code></pre>
  
  <p>Any solutions? </p>