title: How do I chain multiple conditional promises?
body: <p>In my code I have conditional tasks, which all return a promise. I need the tasks to run in sequence.</p>
  
  <p>My current implementation looks something like this:</p>
  
  <pre><code>var chain = [];
  
  if (/* some condition for task A */) {
      chain.push(function(doContinue){
          taskA().then(doContinue);
      });
  }
  
  if (/* some condition for task B */) {
      chain.push(function(doContinue){
          taskB().then(doContinue);
      });
  }
  
  if (/* some condition for task C */) {
      chain.push(function(doContinue){
          taskC().then(doContinue);
      });
  }
  
  var processChain = function () {
      if (chain.length) {
          chain.shift()(processChain);
      } else {
          console.log("all tasks done");
      }
  };
  
  processChain();
  </code></pre>
  
  <p>This works fine, but initially I was looking for a way to create the chain using only Promises and chaining all functions using <code>.then</code>, but I wasn't able to get a working solution.</p>
  
  <p>If there's a cleaner way using only Promises and chains of <code>then</code> calls, then I'd love to see an example.</p>