title: Jquery Deferred in each loop with ajax callback
body: <p>With reference to <a href="https://stackoverflow.com/a/13951699/929894">https://stackoverflow.com/a/13951699/929894</a>, I tried using deferred object in nested ajax loop. However the output is not returning as expected. I have updated my code in fiddle for reference. - <a href="https://jsfiddle.net/fewtalks/nvbp26sx/1/" rel="nofollow noreferrer">https://jsfiddle.net/fewtalks/nvbp26sx/1/</a>. </p>
  
  <p>CODE:</p>
  
  <pre><code>  function main() {
      var def = $.Deferred();
      var requests = [];
      for (var i = 0; i &lt; 2; i++) {
          requests.push(test(i));
      }
      $.when.apply($, requests).then(function() {
          def.resolve();
      });
  
      return def.promise();
  }
  
  function test(x){
      var def = $.Deferred();
    test1(x).done(function(){
                  setTimeout(function(){ console.log('processed test item', x); def.resolve();}, 1000);
            });
    return def.promise();
  }
  
  function test1(items){
      var _d = $.Deferred();
    setTimeout(function(){ 
      console.log('processed test1 item', items); 
      _d.resolve();
    });
    return _d.promise();
  }
  
  main().done(function(){ console.log('completed')});
  </code></pre>
  
  <p>Code contains a main function which executes loop. On each loop, a sub function(test) is executed. Inside the sub function(test) another function(test1) is called.  Both sub functions test and test1 has AJAX call declaration. For AJAX call I have used setTimeout property. I'm expecting an output like </p>
  
  <pre><code>processed test1 item0
  processed test item0
  processed test1 item1
  processed test item0
  completed
  </code></pre>
  
  <p>For each loop, I want the function to be executed as Test1() then test(); However I'm getting the result as</p>
  
  <pre><code>processed test1 item 0
   processed test1 item 1
  processed test item 0
  processed test item 1
  completed
  </code></pre>
  
  <p>After executing the test1 completely test function is executed. Why the function is not executing sequentially for each loop.</p>
  
  <h1>UPdated code for another test run</h1>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function main(items) {
      var items = items;
      return items.reduce(function (p, index) {
          return p.then(function () {
              return test(index);
          });
      }, $.Deferred().resolve());
  }
  
  function test(x) {
      var def = $.Deferred();
      test1(x).done(function () {
          setTimeout(function () {
              log('processed test item', x);
              def.resolve();
          }, 1000);
      });
      return def.promise();
  }
  
  function test1(items) {
      var _d = $.Deferred();
      setTimeout(function () {
          log('processed test1 item', items);
          _d.resolve();
      });
      return _d.promise();
  }
  
  var items = [0, 1];
  function test2(x) {
      var _d = $.Deferred();
      setTimeout(function () {
          log('processed test2 item',x);
          _d.resolve();
      });
      return _d.promise();
  }
  
  main([1,2]).done(function(data){test2(items);}).then(function () {
      log('completed')
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://dl.dropboxusercontent.com/u/7909102/log.js"&gt;&lt;/script&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>why 'completed' is logged before processing test2 function?</p>