# Weather App - How to set global variables from a callback function?

**URL:** <https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430>\
**Category:** General\
**Created:** [November 26, 2016, 8:53pm UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430 "2016-11-26T20:53:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![acljump](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@acljump](https://forum.freecodecamp.org/u/acljump)\
**Post date:** [November 26, 2016, 8:53pm UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/1 "2016-11-26T20:53:02Z")

</div>

For the weather app project, I’m trying to store the latitude and longitude coordinates obtained from the **navigator.geolocation.getCurrentPosition** callback function into global variables.

I’m able to get the coordinate values but when they are assigned to the global variables, the global variables keep remain unassigned. Anyone know what I’m doing wrong?

```
var latitude = 0;
var longitude = 0;

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      latitude = position.coords.latitude;
      longitude = position.coords.longitude;
      // confirmed that position.coords.latitude and position.coords.longitude return non-zero values
    });
}

// latitude and longitude values remain zero
```

---

<div class="post-metadata">

**Author:** ![zsoltime](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zsoltime/32/4252_2.png) [@zsoltime](https://forum.freecodecamp.org/u/zsoltime)\
**Post date:** [November 26, 2016, 9:26pm UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/2 "2016-11-26T21:26:02Z")

</div>

Welcome to the asynchronous world. 🙂

The problem is that you want something before it’s there. You should either work within your callback function or use promises to access the location.

---

<div class="post-metadata">

**Author:** ![acljump](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@acljump](https://forum.freecodecamp.org/u/acljump)\
**Post date:** [November 26, 2016, 9:57pm UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/3 "2016-11-26T21:57:38Z")

</div>

The concept of “promises” hasn’t been introduced by FreeCodeCamp for the weather app project. Is there no way to run the code outside of the callback function without them? In other words, it’s expected to run all the code from within the callback function? I wanted to write “cleaner” code by separate them into functions but that is not possible?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [November 27, 2016, 9:32am UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/4 "2016-11-27T09:32:15Z")

</div>

> [@acljump](#):
>
> The concept of “promises” hasn’t been introduced by FreeCodeCamp for the weather app project.

FreeCodeCamp won’t introduce promises at all (at least not before curriculum expansion, which will happen maybe next year), so you should go ahead and learn them by yourself - it is well worth it.

> [@acljump](#):
>
> Is there no way to run the code outside of the callback function without them?

You could call a function with `latitude` and `longitude` as parameters from the callback and get rid of global variables.

---

<div class="post-metadata">

**Author:** ![Omegga](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/omegga/32/97940_2.png) [@Omegga](https://forum.freecodecamp.org/u/Omegga)\
**Post date:** [November 27, 2016, 2:54pm UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/5 "2016-11-27T14:54:27Z")

</div>

latitude and longitude don’t remain zero forever :

```auto
var latitude = 0;
var longitude = 0;

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      latitude = position.coords.latitude;
      longitude = position.coords.longitude;
      // confirmed that position.coords.latitude and position.coords.longitude return non-zero values
    });
}

setInterval(function() {
  console.log(latitude, longitude)
}, 1000) //=> console logging latitude and longitude every second

// at first, it logs zero values, then non-zero values

```

You don’t have to write the callback code within getCurrentPosition :

```auto
function callback(position) {
   console.log(position.coords.latitude);
}

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(callback);
}

```

---

<div class="post-metadata">

**Author:** ![acljump](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@acljump](https://forum.freecodecamp.org/u/acljump)\
**Post date:** [November 27, 2016, 3:57pm UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/6 "2016-11-27T15:57:00Z")

</div>

I see well thanks. I’ll try to adjust my code to take into consideration this timing issue.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 6:09am UTC](https://forum.freecodecamp.org/t/weather-app-how-to-set-global-variables-from-a-callback-function/59430/7 "2021-01-16T06:09:56Z")

</div>

Closed on January 16, 2021, 6:09am UTC


