title: Recovering from rejected promises in JS
body: <p>I'm using native promises (mostly) and attempting to recover from an error and continue executing the promise chain.</p>
  
  <p>Effectively, I'm doing this:</p>
  
  <ul>
  <li>REST query to see if ID exists. <em>Note that this returns a jquery deferred.</em></li>
  <li>.then (success means ID exists, so fail and stop)
      (fail means ID does not exist, so continue creating ID)</li>
  <li>.then (create the ID record and send to the server)</li>
  </ul>
  
  <p>I return a Promise.resolve() from my rejected function, which should cause the success part of the next .then to execute. It does not. I've tried this on Chrome and Safari.</p>
  
  <p>Note that the first promise is actually a query deferred, but according to this page (<a href="http://api.jquery.com/deferred.then/" rel="nofollow">http://api.jquery.com/deferred.then/</a>), deferred.then() returns a promise object. So adding an extra .then should covert to native promises.</p>
  
  <p>To make it clearer - here's the pseudocode:</p>
  
  <pre><code>promise = $.ajax(url);
  promise = promise.then();  // convert to promise 
  promise.then(function() { cleanup(); return Promise.reject(); },
      function(err) { return Promise.resolve(); });
  .then(function() { createIdentityDetails(); });
  .then(function() { sendIdentityDetails(); });
  </code></pre>
  
  <p>Note that I want to FAIL when the ajax returns success, and I want to 
  continue processing when the ajax call fails.</p>
  
  <p>What happens is that the FAIL functions for all subsequent .then portions execute. That is, my return Promise.resolve() doesn't work - which is (I think) in violation of the spec.</p>
  
  <p>I'd appreciate any feedback on how I can deal with and recover from errors in long promise chains.</p>
  
  <p>Many thanks for any advice you can provide.</p>
  
  <p>p.s. creating and collecting the full identity information is quite time consuming, so I don't want to do it if the ID exists. Hence I want to check first and fail quickly.</p>
  
  <p>p.p.s I really like the way that promises have unwound these deeply nested async callback chains.</p>