How Do I Ensure D3 Finishes Loading Several Csvs Before Javascript Runs?
I'm loading a CSV of a list of other CSV files into javascript using D3. When I run the following code, the employees array is still empty by the time it gets to it in the code. Is
Solution 1:
You could use queue.js to collect the results from all the d3.csv calls:
functionparseList(filenames){
var q = queue();
filenames.forEach(function(d) {
//add your csv call to the queue
q.defer(function(callback) {
d3.csv(d.filename,function(res) { callback(null, res) });
});
});
q.await(restOfCode)
}
functionrestOfCode(err, results) {
//results is an array of each of your csv resultsconsole.log(results)
}
Solution 2:
Your code seems fine. It's just that you're logging employees
before the data is ready. But if you console.log
it on the last line of parseList
you should have it.
Post a Comment for "How Do I Ensure D3 Finishes Loading Several Csvs Before Javascript Runs?"