- Why does example (2) fail, and not retrieve the Array directly?
Because you made two changes, not just one, between your arrow and traditional function approaches.
You changed from arrow function syntax to traditional function syntax, and
You moved the assignment of data
outside the function, assigning it the return value of then
instead of assigning it res
The difference in results was because of the second change, not the first.
- If example (2) does not work, how does example (3) succeed?
Because you undid the change that made it fail, which had nothing to do with function syntax.
- Why does example (4) fail entirely?
It doesn't, but data
is declared within the fulfillment callback, so you can only use it within the fulfillment callback.
- How do I extract the array contained in a PromiseResult/Promise object returned by d3.csv()?
Using either your example (1) or your example (3) (or await
within an async
function or at the top level of a module in environments supporting top-level await
). The result is only available when the promise is fulfilled, not before. You can't get data
as a return value from then
. More:
- How do I make d3.csv() interchangeable between arrow functions and ordinary functions syntax?
See above, it has nothing to do with function syntax.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…