How do I ensure D3 finishes loading several CSVs before javascript runs?

前端 未结 2 661
旧时难觅i
旧时难觅i 2021-02-01 08:01

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

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 08:21

    You could use queue.js to collect the results from all the d3.csv calls:

    function parseList(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)
    }    
    
    function restOfCode(err, results) {
      //results is an array of each of your csv results
      console.log(results)
    }
    

提交回复
热议问题