title: Correct way to write loops for promise.
body: <p>How to correctly construct a loop to make sure the following <strong>promise call</strong> and the chained <strong>logger.log(res)</strong> runs synchronously through iteration? (bluebird)</p>
  
  <pre><code>db.getUser(email).then(function(res) { logger.log(res); }); // this is a promise
  </code></pre>
  
  <p><p>I tried the following way (method from <a href="http://blog.victorquinn.com/javascript-promise-while-loop">http://blog.victorquinn.com/javascript-promise-while-loop</a> )</p>
  
  <pre><code>var Promise = require('bluebird');
  
  var promiseWhile = function(condition, action) {
      var resolver = Promise.defer();
  
      var loop = function() {
          if (!condition()) return resolver.resolve();
          return Promise.cast(action())
              .then(loop)
              .catch(resolver.reject);
      };
  
      process.nextTick(loop);
  
      return resolver.promise;
  });
  
  var count = 0;
  promiseWhile(function() {
      return count &lt; 10;
  }, function() {
      return new Promise(function(resolve, reject) {
          db.getUser(email)
            .then(function(res) { 
                logger.log(res); 
                count++;
                resolve();
            });
      }); 
  }).then(function() {
      console.log('all done');
  }); 
  </code></pre>
  
  <p><p>Although it seems to work, but I don't think it guarantees the order of calling <strong>logger.log(res);</strong> </p>
  
  <p><p>
  Any suggestions?</p>