I am trying to get some data from url to work with in d3.js. I can't seam to get the data using d3.json(url, callback)
. While it works fine when I use (ES8) async await
function.
Can someone help me understand why? What am I doing wrong? Thanks!
d3.json() example:
d3.json("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json", json => {
const data = json.data; // array of dates and values
const lowestVal = d3.min(data, d => d[1]);
const highestVal = d3.max(data, d => d[1]);
console.log(lowestVal);
console.log(highestVal);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
Async await example:
(async function() {
try {
const response = await fetch("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json");
if (response.ok) {
const jsonResponse = await response.json();
const data = jsonResponse.data; // array of dates and values
const lowestVal = d3.min(data, d => d[1]);
const highestVal = d3.max(data, d => d[1]);
console.log(lowestVal);
console.log(highestVal);
}
} catch(error) {
console.log(error);
}
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
I understand there should be some CORS issue here also. But I am working on CodePen for the moment. So CORS is not a problem here.
Many thanks for any insights.