I am using the SharePoint rest web service interface to get a large amount of data from the site.
This involves deeply nested ajax calls to different services (i.e. get website, get it's lists, get all the list's items, for each item, get it's properties, get the webhsite's sub site's... repeat).
I am using an array of promises, pushing each call into the array:
promises.push($.ajax({
url: fullurl,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
$.each(data.d.results, function(i, item) {
ProcessData(i,item, tableTarget, columns, promises);
});
},
error: function (data) {
console.log("request failed: " + fullurl);
}
}));
Each call to ProcessData potentially adds a line to an html table ('tableTarget') and calls for more data.
Ideally I want to delay scripts that attempt to filter the table until all the data is there. The problem is that when I make a call to
var promises = [];
//kickoff my data gathering
AddWebData(url,tableTarget, promises);
//apply
$.when.apply($, promises).then(processFinalTable(promises));
There are only about 2 items in the promises array, so the processFinalTable is called too early. If I delay
setTimeout(function(){
console.log('waited 4000');
$.when.apply($, promises).then(processFinalTable(promises));
}, 4000);
The promises list has 400 or so items. In total I actually need to wait for 1200 odd ajax calls (don't ask).
Given that I am going to make an arbitrary number of calls, depending on the SharePoint site data. How do I determine when the last of my async calls has completed?