4

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);
});
Terry
  • 1,621
  • 4
  • 25
  • 45
  • 1
    Have you seen the [d3.promise](https://github.com/kristw/d3.promise) plugin? – altocumulus Jan 23 '18 at 22:38
  • 1
    Possible duplicate of [How do I convert an existing callback API to promises?](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) – Roamer-1888 Jan 24 '18 at 01:13

2 Answers2

13

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);
PaulMest
  • 12,925
  • 7
  • 53
  • 50
0

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);
            }
         });
     });
 }
Terry
  • 1,621
  • 4
  • 25
  • 45