Convert d3.csv to promise?

泄露秘密 提交于 2021-02-08 06:23:26

问题


Using Angular 5 and I would like to load a CSV file into the async pipe, how do I convert this to a promise?

d3.csv(this.csvFile, function(data) {
      console.log(data);
});

回答1:


Starting with d3 version 5, promises are built in.

d3.csv("file.csv").then(function(data) {
  console.log(data);
});

If you use async/await you can do this:

const data = await d3.csv("file.csv");
console.log(data);



回答2:


With enough googling I worked it out...

loadCSV(file: string) {
    return new Promise(function (resolve, reject){
        d3.csv(file, function(error, request) {
            if(error) {
               reject(error);
            } else {
               resolve(request);
            }
         });
     });
 }


来源:https://stackoverflow.com/questions/48411829/convert-d3-csv-to-promise

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!