Trying to make variable global

Why can’t I see my 2 variables (dataNumAgent, dataNumActAgent) outside of data loading function? I’ve done this before so I must be missing something simple. Help!

let dataNumAgent, dataNumActAgent;

d3.csv("data/Book2.csv").then(function(data) {

  data.forEach(function(d) {
    dataNumAgent = data.filter(d => d.Indicator === "Number of Agents");
    dataNumActAgent = data.filter(
      d => d.Indicator === "Number of Active BB Agents"

console.log(dataNumAgent); //undefined
console.log(dataNumActAgent); //undefined

Not 100% sure, but if I had to guess, it’s asynchronous.

The final two console logs are printed before the promise returned by d3.csv is resolved.

1 Like