0

This is related to this post, but different in that I am trying to understand why I cannot set an outside variable from inside of geolocation.getCurrentPosition

I can't seem to get any kind of return value from it, and I can't set a value from it. I can only access the position object from inside of it. I have tried passing in this.

I forked derek-朕會功夫's fiddle and modified it: https://jsfiddle.net/TwoFistedJustice/c3qr29L7/1/

Here is another fiddle based on this post:

https://jsfiddle.net/TwoFistedJustice/yu8Lzjvx/4/

I'm very perplexed.

Could someone please help me to:

  1. Understand what is going on behind the scenes to make it behave it this way.
  2. Understand how to set a variable in an outer scope from inside getCurrentPosition().
Russ Bain
  • 341
  • 1
  • 4
  • 9

1 Answers1

0

I've been reading up on the call stack and callback queue. And I gather that the behavior that has been perplexing me has to do with how Javascript handles the various stacks and queues. This piece by Jake Archibald helps to clear it up, though it is bit much to absorb all at once.

If I add a setTimeout with any delay value, even 0, foo is USUALLY changed. With near-zero values, it varies a bit.

setTimeout(function(){
console.log('SetTimeout\n -  Foo only changes if this goes AFTER the Promise\n foo:', foo)}

, 0);

This is a fiddle derived from the others that more clearly shows what is going on. https://jsfiddle.net/TwoFistedJustice/arkqb0x8/71/

Russ Bain
  • 341
  • 1
  • 4
  • 9