I have an arbitrarily-size array of urls I need to do AJAX requests to:
var urls = [
"www.whatever.com",
"www.somesite.com/some/path"
];
I need to make AJAX requests to them and output their response to the DOM in the same order as they were specified in the array. I thought this would be a perfect use case for jQuery's $.when()
.
var promises = [];
for (index in urls) {
promises.push($.ajax(urls[index]));
}
$.when(promises).done(function(data){ // <--- I guess you can't do this?
// do something
});
But according to the documentation, you have to pass the deferred functions to it using a comma delimited list.
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
.then( myFunc, myFailure );
This forces you to hard code the number of deferred objects you are sending. In my case, the number of items in my array changes from page to page. Is it possible to pass an array of ajax requests to $.when()
so that the number of ajax requests I send doesn't matter?