I have a simple jQuery script which parses some json files:
var jsonData = {};
$.when(
$.getJSON('data/file1.json', function(data) {
jsonData['file1'] = data;
}),
$.getJSON('data/file2.json', function(data) {
jsonData['file2'] = data;
})
).then(function() {
// do stuff
});
What I would like to do is make this more extensible by being able to loop through a list of files.
I have tried this:
var jsonData = {};
var files = ['file1.json', 'file2.json'];
$.when(
$.each(files, function( index, value ) {
$.getJSON('data/' + value, function(data) {
jsonData[value] = data;
})
})
).then(function() {
// do stuff
});
But then I seem to loose the asynchronous nature of the call.