0

This problem is probably very easy to solve. I just can't seem to solve it. I currently have two functions, and I have to get the data out to the global object, but due to the syntax I'm unfamiliar with how to extract it. The following returns the value I'm looking for.

function position(func) {
    return window.navigator.geolocation.getCurrentPosition(func);
}

position(pos => {
    console.log(pos.coords.latitude);
});

What I need to do is get this out to the global scope. One of my many attempts is the following, which shows up as undefined.

function position(func) {
    return window.navigator.geolocation.getCurrentPosition(func);
}

let lat;

position(pos => {
    lat = pos.coords.latitude;
});

console.log(lat);

Any help would be greatly appreciated.

Aaron Toliver
  • 187
  • 1
  • 3
  • 16
  • Why do you need it in a global scope? getCurrentPosition is asynchronous, so whatever code is depending on this value will need to use callbacks or promises one way or another. – Nicholas Tower Jul 11 '19 at 19:47
  • `window.lat = pos.coords.latitude;` try this – Ivan Karaman Jul 11 '19 at 19:53
  • I need it in a global scope in order to place the data into an action for react-redux. When I formatted it as an action by itself I used async/await in an exported variable. it. It turns out I still need to extract the data and I opted to make it a global variable. Would you like to see that attempt? – Aaron Toliver Jul 11 '19 at 19:58
  • @IvanKaraman I renamed `lat` to `window.lat` within the position function and it still turns up as undefined. Am I assigning that to the wrong variable? – Aaron Toliver Jul 11 '19 at 20:03

0 Answers0