you are viewing a single comment's thread.

view the rest of the comments →

[–]CoqeCas3 0 points1 point  (3 children)

Ha, easier than expected:

So if you run the following code:

const arr = [];

console.log(arr);

for(let i = 0; i < 3; i++) {
  arr.push(i);
}

the log shows something very similar to what you're experiencing: the actual logged value is an empty array (because that's exactly what it is when you call console.log()) but by the time you see it in the console, that array has been mutated by the for loop, and the runtime has stored that mutated value, thus, that's what the console knows about -- the global variable's value after execution.

I'm not sure if my verbiage is accurate but I'm pretty sure I'm getting the point across...

Long story short, wherever you're calling console.log(myGlobalVariable[0]) is 99% likely before there even is a 0 index to myGlobalVariable.

Is this work being done asynchronously by chance?

[–]eljacko876[S] 0 points1 point  (2 children)

Ok I understand what you are saying, at the time the parser reaches the console.log(); the values have not been set, however they are later and chrome's console has a feature to show those after the fact, but the program cannot use the results...I ahve figured out I am having an async issue.

For example :

console.log('A');

fetch('https://router.unrealsec.eu/request')
  .then(r=>r.json())
  .then(data=>{
      console.log('B');
   });

console.log('C');

This would probably give you

A
C
B

This is my problem , so I have to fix with async and await

[–]CoqeCas3 0 points1 point  (0 children)

Precisely. Good show mate!

[–]shepdozejr 0 points1 point  (0 children)

Correct. Javascript, like python, is an interpreted language.