0

currently, my code is returning a promise I need it to return the object that It is getting from the API call, how would do that?

import axios from 'axios';

const baseUrl = 'http://api.openweathermap.org/data/2.5/weather?';

const getWeatherData = async (city,country) => {
    // const result=await axios.get(`http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&APPID=180941f68139fba12f166dc35d9b688b`)
    // return result;
    
    axios({
        method: "GET",
        url: `http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&APPID=180941f68139fba12f166dc35d9b688b`,
      })
        .then((response) => {
          return response.data;
      
        })
        .catch((error) => {
          console.log(error);
        });
}

export default getWeatherData;
mehCoder
  • 15
  • 3
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – zero298 Feb 15 '21 at 19:41

1 Answers1

0
try {
  const response = await axios({
    method: "GET",
    url: `http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&APPID=180941f68139fba12f166dc35d9b688b`,
  });
  return response.data;
} catch (err) {
  console.error(err);
}

You can rewrite your axios call this way since your function is flagged as async.

async functions always return promises. Within async functions, you can use await infront of other async functions or functions that return promises.

Alex Dow
  • 588
  • 1
  • 4
  • 16