AJAX Promises with an Array

I am trying to make several AJAX calls (say 2) using promises. Basically I want to combine the two answers together, do some analysis on them as a whole, and then spit out the answer. Right now I have:

var responseArray = [];
for (var i=0; i<letsSayTwo; i++) {
  responseArray.push(someAjaxCall(data));
};
responseArray.done(function(response) {
  var spit = someAnalysis(response);
  console.log(spit);
});
responseArray.fail(function(response) {
  console.log('fail');
});

Be that as it may, I get “Uncaught TypeError: Object [object Array] does not have a“ done ”error in the console. Do I correctly assume that I cannot use this method? I looked at the following bit of code ( http: // gregfranko. com / blog / jquery-best-practices / ), but I can't get the answer I need.

$.when.apply(this, responseArray).then(function(response) {
  console.log(response);
});

[, "", ], AJAX, . AJAX?

, . !

+2
1

, Uncaught TypeError: Object [object Array] has no method 'done' . , ?

, . Promise Deferred, , $.when.apply(this, responseArray)

... , . [response, "success", response], AJAX, .

docs $.when, - promises (, $.ajax), . response, responseArray.length (letsSayTwo).

AJAX?

( ) arguments, map:

$.when.apply(this, responseArray).done(function() {
  var responses = $.map(arguments, function(args) { return args[0]; }),
      spit = someAnalysis(responses);
  console.log(spit);
}).fail(function(jqXHR, textStatus, errorThrown) {
  console.log('fail: '+textStatus);
});
+2

Source: https://habr.com/ru/post/1658274/


All Articles