title: While loop with promises
body: <p>What would be the idiomatic way to do something like a while loop with promises. So:</p>
  
  <p>do something
  if the condition still stands do it again
  repeat
  then do something else.</p>
  
  <pre><code>dosomething.then(possilblydomoresomethings).then(finish)
  </code></pre>
  
  <p>I've done it this way I was wondering if there were any better/more idomatic ways?</p>
  
  <pre><code>var q = require('q');
  
  var index = 1;
  
  var useless =  function(){
          var currentIndex = index;
          console.log(currentIndex)
          var deferred = q.defer();
          setTimeout(function(){
              if(currentIndex &gt; 10)
                  deferred.resolve(false);
              else deferred.resolve(true);
              },500);
          return deferred.promise;
      }
  
  var control = function(cont){
          var deferred = q.defer();
          if(cont){
                  index = index + 1;
                  useless().then(control).then(function(){
                          deferred.resolve();
                      });
              }
           else deferred.resolve();
          return deferred.promise;
      }
  
  var chain = useless().then(control).then(function(){console.log('done')});
  </code></pre>
  
  <p>Output:
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  done</p>