0

I have two javascript files, one is a global file(global.js) that should be accessed by all other javascript files, and the other uses the variables in global.js

Here's a snippet of the code in global.js:

var datasetArray = [];
        var queue = d3.queue();
        queue.defer(d3.json, "data.json")
            .await(loadJson)
    function loadJson(error, data) {

    //returns the appropriate mapping of the data
        districtDataset = data.map(function(d) {...}); 
datasetArray.push(districtDataset);
    }

In the other js file, I am trying to access the data pushed inside the datasetArray. But the file is being loaded async and I cannot access its objects. So my question is, how can i access the array in the other js files after it the appropriate dataset has been pushed?

Thank you :)

abedzantout
  • 802
  • 4
  • 15
  • 28
  • Would Promises potentially help you work around the asynchronous loading? – James L. Aug 30 '16 at 02:41
  • @JamesLowrey How so? Can you please provide an example as I can't find anything useful online – abedzantout Aug 30 '16 at 13:25
  • Go down to "[Use Promises](stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)" on the accepted answer. However, after doing some more research on D3 it seems like [await takes care of this for you](http://bl.ocks.org/mapsam/6090056). Make sure you are loading the global.js file before the file consuming it. Also, any functions consuming the async JSON files must go into the await callback (where the async operation will be finished. So maybe add a call to the consuming function in loadJson, and load consuming file before global.js? – James L. Aug 30 '16 at 13:32
  • Maybe awaitAll would be helpful? http://stackoverflow.com/questions/20853275/difference-between-queue-await-and-queue-awaitall – James L. Aug 30 '16 at 13:40

0 Answers0