title: Break promise chain and call a function based on the step in the chain where it is broken (rejected)
body: <h3>Update:</h3>
  
  <p>To help future viewers of this post, I created <strong><a href="http://jsbin.com/EpaZIsIp/18/edit" rel="noreferrer">this demo of pluma's answer</a>.</strong> </p>
  
  <h2>Question:</h2>
  
  <p>My goal seems fairly straightforward.</p>
  
  <pre><code>  step(1)
    .then(function() {
      return step(2);
    }, function() {
      stepError(1);
      return $q.reject();
    })
    .then(function() {
  
    }, function() {
      stepError(2);
    });
  
    function step(n) {
      var deferred = $q.defer();
      //fail on step 1
      (n === 1) ? deferred.reject() : deferred.resolve();
      return deferred.promise;
    }
    function stepError(n) {
      console.log(n); 
    }
  </code></pre>
  
  <p>The problem here is that if I fail on step 1, both <code>stepError(1)</code> AND <code>stepError(2)</code> are fired. If I don't <code>return $q.reject</code> then <code>stepError(2)</code> won't be fired, but <code>step(2)</code> will, which I understand. I've accomplished everything except what I'm trying to do.</p>
  
  <p>How do I write promises so that I can call a function on rejection, without calling all of the functions in the error chain? Or is there another way to accomplish this?</p>
  
  <p><a href="http://jsbin.com/EpaZIsIp/3/edit" rel="noreferrer"><strong>Here's a live demo</strong></a> so you've got something work with.</p>
  
  <h2>Update:</h2>
  
  <p>I <strong><em>kind of</em></strong> have solved it. Here, I am catching the error at the end of the chain and passing the data to <code>reject(data)</code> so that I will know what issue to handle in the error function. This actually doesn't meet my requirements because I don't want to depend on the data. It would be lame, but in my case it would be cleaner to pass an error callback to the function rather than to depend on the returned data to determine what to do.</p>
  
  <p><strong><a href="http://jsbin.com/EpaZIsIp/10/edit" rel="noreferrer">Live demo here (click).</a></strong></p>
  
  <pre><code>step(1)
    .then(function() {
      return step(2);
    })
    .then(function() {
      return step(3);
    })
    .then(false, 
      function(x) {
        stepError(x);
      }
    );
    function step(n) {
      console.log('Step '+n);
      var deferred = $q.defer();
      (n === 1) ? deferred.reject(n) : deferred.resolve(n);
      return deferred.promise;
    }
    function stepError(n) {
      console.log('Error '+n); 
    }
  </code></pre>