title: How to handle error properly in Promise chain?
body: <p>Say we have 3 asynchronous tasks that return Promises: <code>A</code>, <code>B</code> and <code>C</code>. We want to chain them together <em>(that is, for sake of clarity, taking the value returned by <code>A</code> and calling <code>B</code> with it)</em>, but also want to handle the errors correctly for each, and break out at the first failure. Currently, I see 2 ways of doing this:</p>
  
  <pre><code>A
  .then(passA)
  .then(B)
  .then(passB)
  .then(C)
  .then(passC)
  .catch(failAll)
  </code></pre>
  
  <p>Here, the <code>passX</code> functions handle <em>each</em> of the success of the call to <code>X</code>. But in the <code>failAll</code> function, we'd have to handle <em>all</em> of the errors of <code>A</code>, <code>B</code> and <code>C</code>, which may be complex and not easy to read, especially if we had more than 3 async tasks. So the other way takes this into consideration:</p>
  
  <pre><code>A
  .then(passA, failA)
  .then(B)
  .then(passB, failB)
  .then(C)
  .then(passC, failC)
  .catch(failAll)
  </code></pre>
  
  <p>Here, we separated out the logic of the original <code>failAll</code> into <code>failA</code>, <code>failB</code> and <code>failC</code>, which seems simple and readable, since all errors are handled right next to its source. <strong>However, this does not do what I want.</strong></p>
  
  <p>Let's see if <code>A</code> fails (rejected), <code>failA</code> must not proceed to call <code>B</code>, therefore must throw an exception or call reject. But both of these gets caught by <code>failB</code> and <code>failC</code>, meaning that <strong><code>failB</code> and <code>failC</code> needs to know if we had already failed or not</strong>, presumably by keeping state (i.e. a variable). </p>
  
  <p>Moreover, it seems that the more async tasks we have, either our <code>failAll</code> function grows in size (way 1), or more <code>failX</code> functions gets called (way 2). This brings me to my question:</p>
  
  <p><strong>Is there a better way to do this?</strong></p>
  
  <p>Consideration: Since exceptions in <code>then</code> is handled by the rejection method, should there be a <code>Promise.throw</code> method to actually break off the chain?</p>
  
  <p>A <a href="https://stackoverflow.com/questions/28803287/how-to-break-promise-chain">possible duplicate</a>, with an answer that adds more scopes inside the handlers. Aren't promises supposed to honor linear chaining of functions, and not passing functions that pass functions that pass functions?</p>