title: $.when.apply on Array of Promises
body: <p>I have searched many very similar questions. I found one that had almost exactly what I was looking for except it did not have any error handling, and upon trying to implement error handling I am running into some issues.</p>
  
  <p>My goal is to create a function that receives an array of names, creates $.ajax requests in a loop pushing the returns promise objects into an array which I then provide to $.when.apply($,arrayOfPromises).then(// etc to resolve a master deferred where all of the promises in arrayOfPromises have been resolved. The end result is to create an array of the data returned from the ajax calls.</p>
  
  <p>I have this so far: <a href="http://jsfiddle.net/BGx2h/1/" rel="nofollow">http://jsfiddle.net/BGx2h/1/</a></p>
  
  <p>It's almost exactly what I need. If the array contains pointers to all valid resources, everything seems to work as intended. However, if there is a call to a resource the doesn't exist (such as /test/doesnt.exist.txt in my fiddle), the ajax call for "doesnt.exist" finishes before any of the other calls, the master deferred (connectiondfd) gets resolved before the others calls have a chance to finish (you may have to run the fiddle several times to get this result). Here is what my function looks like currently:</p>
  
  <pre><code>function multiAsync() {
      var i,data=[],connections=[],
          targets=['John.Smith','Jane.Doe','Bob.Someone','doesnt.exist'];
  
      var connectiondfd = $.Deferred();
  
      for(i=0;i&lt;targets.length;i++) {
          connections.push($.ajax({
              url:'http://porticium.ca/test/'+targets[i]+'.txt',
              type:'GET',
              async:true,
              timeout:5000
          }).then(function(newData) {
              data.push(newData);
          },function() {
              data.push("NO DATA");
          }));
  }
  
      $.when.apply($,connections)
          .then(
              function() { connectiondfd.resolve(); },
              function() { connectiondfd.resolve(); }
          );
  
  
      $.when(connectiondfd).done(function() {
          alert("FINAL: " + data);
      });
  }
  </code></pre>
  
  <p>Would definitely appreciate some help on this one, it has been driving me insane!</p>
  
  <p>Thanks,
  Rob</p>