The below function is supposed to use load in a set of JSON files into javascript objects and return them as an array. From my debug I can see that this is all working and doing as it is supposed to although it is executing the load: function(data)
last after all other Javascript has finished executing.
So the function executes. Runs the xhr.get 15 times. Returns currentDataSet (empty). Only then the currentDataSet.push(data);
within load: function(data)
executes populating currentDataSet. At which point it is too late as the return
has already run.
require(['dojo/_base/xhr', "dojo/dom", "dojo/date/stamp"], function(xhr, dom){
generateDataSet = function (startTime) {
var dataSetFilesNames = generateFilenameSet(startTime);
var currentDataSet = [];
for(var j=0; j<15; j++){
xhr.get({
url:("/JSONFiles/" + dataSetFilesNames[j]), handleAs:"json",
load: function(data){
//The .push statement below is executed 15 times at the end
currentDataSet.push(data);
}
});
}
return currentDataSet;
}
});
I'll admit this is all new territory for me and I may be misunderstanding how the xhr.get works. I guess the load
function executes as the responses come back from the requests. Is there any way that I can get the desired function above to execute in the right order? A way to wait for the response of each xhr.get etc. Open to suggestions.
Thanks in advance.