0

I am passing an arbitrary amount of deferred get requests to $.when.apply(this, ajax_calls).done(function() {}); but I do not know how to access the arguments usually passed to the callback of each ajax_call.

I want to access the results of each ajax call in the .done() function, maybe in the form:

$.when.apply(this, ajax_calls).done(function(resultArray) {
    resultArray.forEach( function(s) {
         console.log(s)
    }
});

or something similar.

Is this possible?

Rory Byrne
  • 923
  • 1
  • 12
  • 22
  • see the documentation of $.when ... https://api.jquery.com/jquery.when/ ... .done function will get 1 argument per deferred – Jaromanda X Sep 10 '15 at 14:30
  • See also [this approach for ajax promises](http://stackoverflow.com/a/19090635/1048572) – Bergi Sep 10 '15 at 14:34
  • I know how to access the deferred results normally, but if I pass an arbitrary amount of deferred calls, then I won't know how many arguments the `done` function will get. Is there a way to access the `done` arguments when I pass an array of args using `$.when.apply()`? – Rory Byrne Sep 10 '15 at 14:41
  • Both of the questions I linked show you how to use the `arguments` object for a variable amount of results… – Bergi Sep 10 '15 at 14:46
  • I hadn't seen your answer @Bergi. In the answer you linked above: is `arguments` an array of result arrays that `$.when` returns? Is `args` then each of the arrays inside that array? Lastly, should `response` be `responses`, or are the two different variables? If they're different, then what is going on there? Cheers. – Rory Byrne Sep 10 '15 at 14:52
  • `arguments` is something like an array, yes. Whether it contains arrays or values directly depends whether your original promises resolve with multiple values (like `$.ajax()` ones do) or not. – Bergi Sep 10 '15 at 15:08

0 Answers0