I am trying to understand the interchangeability of arrow functions vs ordinary functions for d3.csv() calls and promises.
Using d3.csv(), I can successfully return and load a CSV file with arrow function syntax, which returns arrays:
let fp = "data_file.csv" let data = d3.csv(fp).then( res => data = res) > Array(30)
However, I am not able to load the same using ordinary function syntax - this actually returns a Promise object. I do not know how to extract the results from the object, but the PromiseResult seems to contain the array as above:
let fp = "data_file.csv" let data = d3.csv(fp).then( function(res) { return res }) > Promise
But if I declare 'data' first, and execute a d3.csv call and bind data within it, I successfully get the array data directly from d3.csv(), as in (1) above:
let data; d3.csv(fp).then( function(res) { data = res }) > Array(30)
Yet if I declare/define data within d3.csv(), the code entirely fails:
d3.csv(fp).then( function(res) { let data = res }) > ReferenceError: data is not defined at <anonymous>:1:1
Questions:
Why does example (2) fail, and not retrieve the Array directly?
If example (2) does not work, how does example (3) succeed?
Why does example (4) fail entirely?
How do I extract the array contained in a PromiseResult/Promise object returned by d3.csv()?
How do I make d3.csv() interchangeable between arrow functions and ordinary functions syntax?
I've looked at various d3.csv()-related questions and promise-related questions, but none seem to address this issue specifically. In particular, these two questions don't answer what I've posed.