Load multiple files using the d3-fetch module

前端 未结 1 409
遥遥无期
遥遥无期 2020-12-11 03:34

I try to load data from two different sources. After loading the data I want to use it within a riot tag file. But I do not understand how to load the second file, because I

相关标签:
1条回答
  • 2020-12-11 03:43

    The d3-fetch module makes use of the Fetch API and will, therefore, return a Promise for each request issued via one of the module's convenience methods. To load multiple files at once you could use Promise.all which will return a single Promise that resolves once all Promises provided to the call have resolved.

    import { csv, json } from 'd3-fetch'
    
    Promise.all([
      csv('/data/stations.csv'),
      json('data/svg_data.json')
    ])
    .then(([stations, svg]) =>  {
      // Do your stuff. Content of both files is now available in stations and svg
    });
    

    Here, d3.csv and d3.json are provided to fetch content from two files. Once both requests have completed, i.e. both Promises have resolved, the content of each file is provided to the single Promise's .then() method call. At this point you are able to access the data and execute the rest of your code.

    0 讨论(0)
提交回复
热议问题