title: Chaining jQuery deferred by updating deferred inside then()?
body: <p>I am learning to use jQuery Deferred and I am thinking about chaining multiple deferreds. Here is a simplified version of my problem:</p>
  
  <pre><code>var def1 = $.ajax(...); // ajax call 1
  var def2 = null, def3 = null;
  
  $.when(def1)
    .then(function() {
      def2 = $.ajax(...); // ajax call 2
      return def2;
    }).then(function() {
      def3 = $.ajax(...); // ajax call 3
      return def3;
    });
  </code></pre>
  
  <p>Then somewhere below the above code, I have:</p>
  
  <pre><code>$.when(def1, def2, def3)
    .then(function() {
      // run some code... .e.g. run foo();
    });
  </code></pre>
  
  <p>The above code is wrong. It can't do what I want. My actual intention is basically:</p>
  
  <p>I want to run ajax call 1 first. </p>
  
  <p>If ajax call 1 is successful, then run ajax call 2. (Would be a bonus to add on: If ajax call 1 is not successful and/or its returned result doesn't pass my manual validation, alert the user and run some code, e.g. bar().)</p>
  
  <p>If ajax call 2 is successful, then run ajax call 3. (same bonus applies)</p>
  
  <p>And then later I want to run some code ( e.g. foo() ) only when def3 has been resolved. Before running foo(), I don't actually care if def1 and def2 has been resolved or not.
  (If def3 is rejected, I want to run another piece of code too, if possible. )</p>
  
  <p>The reason that I can't initialise def2(ajax call 2) and def3(ajax call 3) at the beginning is that I want to modify the options of ajax call 2 depending on the returned result from ajax call 1. The Same manner for ajax call 3, depending on ajax call 2's result.</p>
  
  <p>With my limited knowledge and understanding of deferred, the above code basically can't produce what I want. </p>
  
  <p>Because def1 is resolved before def2 is initialised and fired, so the bottom piece of code will run foo() way too soon before def2 is initiated...</p>
  
  <p>So, I am asking for help and advice. See if anyone has any idea how to rewrite the code above and make it run like my intention written above. I am sorry for my incapability.</p>
  
  <p>Thank you all in advance. </p>