Exploring Scope in JavaScript:
var myData = data.map(function(d){
q.defer(d3.csv,'https://***/'+d+'.csv', d);
return d
})
q.awaitAll(function(error, result, d) {
if (error) throw error;
// Accessing the value of d here can be achieved through the parent scope.
console.log(myData) // The myData variable holds all values of d from your map function.
});
Furthermore, when using data.map()
, since data
is already utilized (data
being bound to d
within the mapper function):
q.awaitAll(function(error, result, d) {
if (error) throw error;
// Accessing the value of d here can be done by referring to data directly.
console.log(data) // This refers to the same `data` used in `data.map()`
});
NOTE: For clarification, the query has been updated indicating a desire to reassign the results back to a d
value:
q.awaitAll(function(error, result) {
if (error) throw error;
// Obtain the value of d at this point.
result.map(function(res, idx) {
console.log("original d:" + data[idx].toString())
}
});
For more information on awaitAll functionality, refer to the documentation provided: https://github.com/d3/d3-queue#queue_awaitAll
Enhance your understanding with these additional resources:
https://scotch.io/tutorials/understanding-scope-in-javascript