# Async Function Help

**URL:** <https://forum.freecodecamp.org/t/async-function-help/387867>\
**Category:** General\
**Created:** [May 12, 2020, 9:10pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867 "2020-05-12T21:10:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bstefansen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bstefansen/32/143007_2.png) [@bstefansen](https://forum.freecodecamp.org/u/bstefansen)\
**Post date:** [May 12, 2020, 9:10pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/1 "2020-05-12T21:10:09Z")

</div>

Can someone explain to me why my async function is not awaiting for my getLocation function to finish?

```auto
var url = "https://fcc-weather-api.glitch.me/api/current?";
var urlTest = "https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139";
var newURL = url + 'lat=' + lat + '&lon=' + lon;
var lon;
var lat;
var name;

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      lon = parseFloat(position.coords.longitude);
      lat = parseFloat(position.coords.latitude);
      document.getElementById('location').innerHTML = "latitude: " + lat + "<br>longitude: " + lon;
    });
  }
}

function getWeather() {
  return fetch(newURL)
    .then(res => res.json())
    .then(data => 
      name = data.name
    )
}

// await is not working
async function App() {
  await getLocation();
  await getWeather();

  document.getElementById('weather').innerHTML = name
}

App()

```

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [May 12, 2020, 9:15pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/2 "2020-05-12T21:15:34Z")

</div>

Because it is not returning a Promise.

---

<div class="post-metadata">

**Author:** ![bstefansen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bstefansen/32/143007_2.png) [@bstefansen](https://forum.freecodecamp.org/u/bstefansen)\
**Post date:** [May 12, 2020, 9:26pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/3 "2020-05-12T21:26:00Z")

</div>

Thank you for the feedback. So this is what I’ve come up with, but its still not working:

```auto
var url = "https://fcc-weather-api.glitch.me/api/current?";
var urlTest = "https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139";
var newURL = url + 'lat=' + lat + '&lon=' + lon;
var lon;
var lat;
var name;

var getLocation = new Promise((resolve) => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      lon = parseFloat(position.coords.longitude);
      lat = parseFloat(position.coords.latitude);
      document.getElementById('location').innerHTML = "latitude: " + lat + "<br>longitude: " + lon;
    });
  }

  resolve();
})

var getWeather = new Promise((resolve) => {
  fetch(newURL)
    .then(res => res.json())
    .then(data => 
      name = data.name
  )

  resolve();
})

// await is not working
async function App() {
  await getLocation;
  await getWeather;

  document.getElementById('weather').innerHTML = name
}

App()

```

---

<div class="post-metadata">

**Author:** ![rjost1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rjost1/32/145403_2.png) [@rjost1](https://forum.freecodecamp.org/u/rjost1)\
**Post date:** [May 12, 2020, 10:08pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/4 "2020-05-12T22:08:53Z")

</div>

Try assigning the await functions to variables (respectively) and then call the property.  
async function App() {  
const location = await getLocation;  
const weather = await getWeather;

document.getElementById(‘weather’).innerHTML = weather.name;  
}

---

<div class="post-metadata">

**Author:** ![bstefansen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bstefansen/32/143007_2.png) [@bstefansen](https://forum.freecodecamp.org/u/bstefansen)\
**Post date:** [May 12, 2020, 10:39pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/5 "2020-05-12T22:39:21Z")

</div>

I see. Thank you! Final question. The urlTest seems to work in the getWeather function, but newURL does not?

```auto
var url = "https://fcc-weather-api.glitch.me/api/current?";
var urlTest = "https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139";
var newURL;

var getLocation = new Promise((resolve) => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      let lon = position.coords.longitude;
      let lat = position.coords.latitude;
      document.getElementById('location').innerHTML = "latitude: " + lat + "<br>longitude: " + lon;
      newURL = url + 'lat=' + lat + '&lon=' + lon;
    });
  }

  resolve();
})

// newURL is undefined but urlTest works
var getWeather = 
  fetch(/*urlTest/ newURL*/)
    .then(res => res.json())
    .then(data => JSON.stringify(data))
    
async function App() {
  const location = await getLocation;
  const weather = await getWeather;
  document.getElementById('test').innerHTML = newURL
  document.getElementById('weather').innerHTML = weather
}

App()

```

---

<div class="post-metadata">

**Author:** ![rjost1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rjost1/32/145403_2.png) [@rjost1](https://forum.freecodecamp.org/u/rjost1)\
**Post date:** [May 12, 2020, 11:22pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/6 "2020-05-12T23:22:32Z")

</div>

newURL is only defined within the getLocation function.  
urlTest works because it’s been declared globally.

You want the getLocation function to return the lat and long data.

var newURL;

var getLocation = new Promise((resolve) =\> {  
if (navigator.geolocation) {  
navigator.geolocation.getCurrentPosition(function(position) {  
let lon = position.coords.longitude;  
let lat = position.coords.latitude;  
document.getElementById(‘location’).innerHTML = "latitude: " + lat + "  
longitude: " + lon;

```
});

```

}

resolve();  
return { lat, long };  
})  
newURL = url + ‘lat=’ + getLocation.lat + ‘&lon=’ + getLocation.lon;

I think this is formatted correctly.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:53pm UTC](https://forum.freecodecamp.org/t/async-function-help/387867/7 "2021-06-01T18:53:52Z")

</div>

Closed on June 1, 2021, 6:53pm UTC


