I am trying to simply add some objects from this callback function to a global array for use later. I am running into an issue with the asynchronous nature of the code and I don't understand why it isn't working.
For both arrays dl and arr, I cannot access their indices and the console claims their length is 0. However, in chrome dev-tools I can view a dropdown of about 1000 objects in the array, as should be the case.
dl[0] or arr[0] returns undefined, for example.
I should mention this is all within a componentDidMount() in my React app. The reason I am making arrays, as opposed to just setting state is that the function is embedded and within a get method here and the indices need to be accesed later anyhow.
let dl =[];
let arr=[];
_globalobject.get(
"/SOME/URL.PARAMETERS/HERE",
function(response) {
// authenticated user
_globalobject.user = response;
for (let i=0; i < response.results.length; i++) {
dl.push(response.results[i])
arr.push(i)
}
}
)
console.log(dl, typeof(dl), dl.length) // [] > , 'object', 0
console.log("regular array", arr[0]) // regular array, undefined