1

I am making a fetch API call from my js file ,I have a doubt that when i am using Async/await still the code is executing in asynchronous manner.

I have also tried await at differnt place but its doesn't work.

 let info
 async function weather(){
 // API call
 let data=await fetch('http://api.openweathermap.org/data/2.5/weather?'+'&lat=20&lon=44'+'&units=metric'+'&APPID='+WEATHER_KEY)
  console.log("inside API")
  const res= await data.json();
  console.log(res)
 }
weather()
console.log("last part")

OUTPUT:

last part

inside API

"VALUE OF RES"

WHAT I EXPECT:

inside API

"VALUE OF RES"

last part

Any help will be highly appreciated..

5
  • 3
    JavaScript is asynchronous, the "last part" runs first because the await hasn't returned a result yet. Commented Jan 3, 2019 at 15:50
  • 2
    you'll need to await weather. Commented Jan 3, 2019 at 15:50
  • And to await weather() the whole things needs to be inside another async function. Commented Jan 3, 2019 at 15:51
  • add await to weather function Commented Jan 3, 2019 at 15:52
  • As you wrote it yourself, weather() is asynchronous. Meaning non-blocking, meaning console.log("last part") won't wait for it to complete. Commented Jan 3, 2019 at 15:52

3 Answers 3

1

The easiest way is to wrap it all in another async function so that you can await weather().

// this function fetches the weather then logs the result
async function weather() {
  // API call
  let data = await fetch('http://api.openweathermap.org/data/2.5/weather?'+'&lat=20&lon=44'+'&units=metric'+'&APPID='+WEATHER_KEY);
  console.log('inside API');
  const res = await data.json();
  console.log(res);
}

// this async function awaits for weather() to return
async function run() {
  await weather();
  console.log('last part');
}

// this runs first
run();
Sign up to request clarification or add additional context in comments.

Comments

0
// async function
async function fetchAsync () {
  // await response of fetch call
  let response = await fetch('http://api.openweathermap.org/data/2.5/weather?'+'&lat=20&lon=44'+'&units=metric'+'&APPID='+WEATHER_KEY);
  // only proceed once promise is resolved
  let data = await response.json();
  // only proceed once second promise is resolved
  return data;
}

// trigger async function
// log response or catch error of fetch promise
fetchAsync()
    .then(data => console.log(data))
    .catch(err => console.log(err))

Comments

0

A solution in a script that run in node environment:

(async function () {

  // API call
  const data = await fetch('http://api.openweathermap.org/data/2.5/weather?'+'&lat=20&lon=44'+'&units=metric'+'&APPID='+WEATHER_KEY)
  console.log("inside API")

  const res = await data.json();
  console.log(res)

  console.log("last part")
})()

The messages will are in expected order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.