0

I'm trying to extract data from an online .csv file and when I do my function returns a promise. What I would like to do is take data from the promise and append (or push) it to an array so that I can use it in my visualization.

the issue I'm facing is dataFinal is empty and someData only contains values inside the .then() function.

Any tips on how to get data into my arrays would be much appreciated!

Here's what I currently have:

async function getData() {
        const nytdata = await d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv")
    return nytdata
    }

let nytdata = getData();
var someData = [];
var datafFinal =  someData.then(function(result) {
    for (var i = 0; i < data.length; i++) {
      someData.push({
        date: data[i]["date"],
        value: data[i]["cases"],
        group: data[i]["date"].getMonth()
      });
    }
    return someData
});
console.log(datafFinal) 
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `data` is undefined. I don't see it being declared – Rojo Jan 23 '21 at 20:07
  • That is the only way to get data out. Async is async all the way down. – zero298 Jan 23 '21 at 20:13
  • Did you mean `nytdata.then()`? – Barmar Jan 23 '21 at 20:17
  • And `data` should probably be `result` – Barmar Jan 23 '21 at 20:18
  • 1
    Please take a look at the answers here (scroll down past the ones about jquery): [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). The array isn't your issue, the issue is that you're trying synchronous calls on data that depends on async calls. Once you've got a Promise, you need to keep using promises (or async/await) through the rest of that logic. – Zac Anger Jan 23 '21 at 20:19
  • `console.log(datafFinal)` is being executed before the callback. – Barmar Jan 23 '21 at 20:19

0 Answers0