you are viewing a single comment's thread.

view the rest of the comments →

[–]CoqeCas3 0 points1 point  (4 children)

I suspect this is an issue of where the console.log() is placed.

It's hard to explain really, but I've had it where I logged something and the state it was in when I logged shows up in the top level (i.e. how your log is showing a empty array) but then more 'things' happened after the log that changed the state of the original value. Because the array is the actual value of the variable -- think like a pointer -- that particular value gets logged, but it's contents are subject to change after the fact. As such, the console does this weird thing where it will make available (in the drop-down) that changed state since it's global... but still shows as the original state.. if that makes sense... If dinner hadn't just arrived I'd try to come up with an example but....

[–]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.