Currently, I am working on loading files asynchronously by utilizing d3's queue, defer, and await features. The issue arises when I attempt to execute this process in a loop, where for each iteration, I aim to store the retrieved data in a dictionary:
var myDictionary = {};
// For example, hierarchy = ["State", "County"]
hierarchy.forEach(function(geo) {
queue()
.defer(d3.json, 'assets/data/geo/' + geo + '/' + geo + '.json')
.defer(d3.csv, 'assets/data/geo/' + geo + '/' + geo + '_info.csv')
.await(myFunc);
});
function myFunc(error, jsonData, csvData) {
// It is crucial to have access to the 'geo' variable
console.log(geo);
myDictionary[geo].jsonData = jsonData;
myDictionary[geo].csvData = csvData;
}
Within myFunc, the challenge lies in obtaining access to 'geo' in order to correctly populate the dictionary keys. Passing it through .await.myFunc(geo)
results in 'undefined' being printed inside myFunc.
I am uncertain whether this dilemma is related to Javascript's callback functions, D3's await() method, or both.
Do you have any suggestions or recommendations?
Thank you