I've got some code which makes requests to two different files on my web server, and I'd like to take some action after they're done, leveraging deferreds:
var url = 'testDataRoot.json',
folderUrl= 'Content/testData.json';
var root = $.ajax({
url: url,
type: 'GET',
dataType: 'json'
})
var folder = $.ajax({
url: folderUrl,
type: 'GET',
dataType: 'json'
})
function getBiggest(arr) {
arr.sort(function(a,b) {
return a < b;
})
return arr[0];
}
$.when
takes a list/array of deferred:
$.when([root,folder]).then(function(args){
console.log(args); // promise array
console.log(args[0]); // first promise
console.log(args[0]['responseText']); // this has a value in the two previous lines yet undefined here, why?
});
I'd like to call getBiggest
on the response from each deferred. I'd like a solution that would work for two deferreds, or 200. What's the most extensible way to accomplish this?