title: jQuery deferred object with nested ajax calls
body: <p>I have a situation in which my ajax calls must perform in a particular order. I have used jQuery Deferred objects in other situations, but cannot seem to find a way to make this behave appropriately.</p>
  
  <p>I have a function which performs a number of <code>ajax</code> requests in it's lifetime. Some of the requests will be performed during the success callback of other requests.</p>
  
  <p>My question: <strong>is there a way to return all nested deferred objects to the original <code>$.when</code> call?</strong></p>
  
  <p>A simplified example would be:</p>
  
  <pre><code>function nestedAjax() {
      $.get("/", function(){
          console.log("First ajax done.");
          $.get("/", function(){
              console.log("Second ajax done.");
          });
      });
  };
  </code></pre>
  
  <p>I am trying to have the <code>nestedAjax</code> function to use <code>$.when()</code> and <code>$.done()</code> like so:</p>
  
  <pre><code>$.when(nestedAjax()).done(function(){
      console.log("Complete");
  });​
  </code></pre>
  
  <p>With the console output reading:</p>
  
  <pre><code>&gt; First ajax done.
  &gt; Second ajax done.
  &gt; Complete.
  </code></pre>
  
  <p>I can return the first <code>get</code> to achieve this:</p>
  
  <pre><code>&gt; First ajax done.
  &gt; Complete.
  &gt; Second ajax done.
  </code></pre>
  
  <p>But obviously this is not what I require. Any help would be appreciated.</p>