0

I have the following code:

var requests = [];

var files = ['one.html', 'two.html', 'three.html'];

for(var i = 0; i<files.length; i++){
        requests.push($.get(files[i]));
}

$.when.apply(undefined, requests).
then(function(resultOne, resultTwo, resultThree){
    console.log(resultOne[0]);
})

I wish to avoid having to define variable for each response in the .then and instead retrieve an array of responses.

How should I go about this?

Thank you!

jacobdo
  • 1,605
  • 3
  • 15
  • 34

1 Answers1

3

In every function scope there is a special array like object in its scope called arguments which corresponds to the values passed to the function call.

You can use the arguments object and iterate over it

$.when.apply(undefined, requests).
then(function(resultOne, resultTwo, resultThree) {
  $.each(arguments, function(i, val) {
    console.log(val);//it will be another array with 3 values
    console.log(val[0]);//to get the data returned by the ajax request
  })
})
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531