How to build modular js promise chains?
<p>I have a promise chain that is currently running with very basic error handling. If a step fails, the subsequent steps will not run. I want to be able to continue the promise chain at a later time if there is a failure, and it should start from where it left off.</p>

<p>This is what my current code looks like:</p>

<pre><code>// controller.js
var failure = false;
QueryFactory.step1()                      // Step 1
   .then(function(msg){
      if(failure == false){
        console.log("Finished Step 1");
        return QueryFactory.step2();
      }
   }, function(err){
      console.log(err);
      failure = true;
   })
   .then(function(msg){                   // Step 2
      if(failure == false){
         console.log("Finished Step 2");
         return QueryFactory.step3();
      }
   }, function(err){
      console.log(err);
      failure = true;
   })
   .then(function(msg){                   // Step 3
      if(failure == false){
         console.log("Finished Step 3");
      }
   }, function(err){
      console.log(err);
      failure = true;
   })
</code></pre>

<p>And my factory looks like this:</p>

<pre><code>// QueryFactory.js
step1 = function(){
   var deferred = $q.defer();
   $http.post('/task', {step_num: 1})
      .then(function(data)){
         deferred.resolve(data);
      }, function(err){
         deferred.reject(err);
      });
   return deferred.promise;
}
// step2 and step3 are similar
</code></pre>

<p>But perhaps there is a better way of writing this to allow for modularity? Say step2 were to fail, how can I design a way so that a user can click a button to later continue the chain from step2? </p>