title: Chained promises not passing on rejection
body: <p>I am have a problem understanding why rejections are not passed on through a promise chain and I am hoping someone will be able to help me understand why. To me, attaching functionality to a chain of promises implies an intent that I am depending on an original promise to be fulfilled. It's hard to explain, so let me show a code example of my problem first. (Note: this example is using Node and the deferred node module. I tested this with Dojo 1.8.3 and had the same results)</p>
  
  <pre><code>var d = require("deferred");
  
  var d1 = d();
  
  var promise1 = d1.promise.then(
      function(wins) { console.log('promise1 resolved'); return wins;},
      function(err) { console.log('promise1 rejected'); return err;});
  var promise2 = promise1.then(
      function(wins) { console.log('promise2 resolved'); return wins;},
      function(err) { console.log('promise2 rejected'); return err;});
  var promise3 = promise2.then(
      function(wins) { console.log('promise3 resolved'); return wins;},
      function(err) { console.log('promise3 rejected'); return err;});
  d1.reject(new Error());
  </code></pre>
  
  <p>The results of running this operation is this output:</p>
  
  <pre><code>promise1 rejected
  promise2 resolved
  promise3 resolved
  </code></pre>
  
  <p>Okay, to me, this result doesn't make sense. By attaching to this promise chain, each then is implying the intent that it will be dependant upon the successful resolution of d1 and a result being passed down the chain. If the promise in promise1 doesn't receive the wins value, but instead gets an err value in its error handler, how is it possible for the next promise in the chain to have its success function called? There is no way it can pass on a meaningful value to the next promise because it didn't get a value itself.</p>
  
  <p>A different way I can describe what I'm thinking is: There are three people, John, Ginger, and Bob. John owns a widget shop. Ginger comes into his shop and requests a bag of widgets of assorted colours. He doesn't have them in stock, so he sends in a request to his distributor to get them shipped to him. In the mean time, he gives Ginger a rain check stating he owes her the bag of widgets. Bob finds out Ginger is getting the widgets and requests that he get the blue widget when she's done with them. She agrees and gives him a note stating she will. Now, John's distributor can't find any widgets in their supply and the manufacturer doesn't make them any more, so they inform John, who in turn informs Ginger she can't get the widgets. How is Bob able to get a blue widget from Ginger when didn't get any herself?</p>
  
  <p>A third more realistic perspective I have on this issue is this. Say I have two values I want updated to a database. One is dependant on the id of the other, but I can't get the id until I have already inserted it into a database and obtained the result. On top of that, the first insert is dependant on a query from the database. The database calls return promises that I use to chain the two calls into a sequence. </p>
  
  <pre><code>var promise = db.query({parent_id: value});
  promise.then(function(query_result) {
      var first_value = {
          parent_id: query_result[0].parent_id
      }
      var promise = db.put(first_value);
      promise.then(function(first_value_result) {
          var second_value = {
              reference_to_first_value_id: first_value_result.id
          }
          var promise = db.put(second_value);
          promise.then(function(second_value_result) {
              values_successfully_entered();
          }, function(err) { return err });
      }, function(err) { return err });
  }, function(err) { return err });
  </code></pre>
  
  <p>Now, in this situation, if the db.query failed, it would call the err function of the first then. But then it would call the success function of the next promise. While that promise is expecting the results of the first value, it would instead get the error message from its error handler function.</p>
  
  <p>So, my question is, why would I have an error handing function if I have to test for errors in my success function? </p>
  
  <p>Sorry for the length of this. I just didn't know how to explain it another way.</p>
  
  <p><strong>UPDATE and correction</strong></p>
  
  <p>(Note: I removed a response I had once made to some comments. So if anyone commented on my response, their comments might seem out of context now that I removed it. Sorry for this, I am trying to keep this as short as possible.)</p>
  
  <p>Thank you everybody who replied. I would like to first apologize to everybody for writing out my question so poorly, especially my pseudo code. I was a little too aggressive in trying to keep it short.</p>
  
  <p>Thanks to Bergi's response, I think I found the error in my logic. I think I might have overlooked another issue that was causing the problem I was having. This is possibly causing the promise chain work differently than I thought it should. I am still testing different elements of my code, so I can't even form a proper question to see what I'm doing wrong yet. I did want to update you all though and thank you for your help.</p>