0

When I console.log(data), I log the information I need, but if return the value of data in getWeather(), it just returns a pending promise. I have tried many things, but none have worked so far. I'll leave my broken code below.

const axios = require('axios');
const getWeather = async () => {
    try {
        let response = await axios.get(
            'http://api.openweathermap.org/data/2.5/forecast?id=524901&appid={apiKey}'
        );

        let data = response.data;
        return data;
    } catch (e) {
        console.log(e);
    }
};

async function returnAsync() {
    const x = await getWeather();
    return x;
}
console.log(getWeather()); // returns a pending promise
console.log('check ', returnAsync()); // also returns a pending promise
  • 1
    `async` functions return a Promise; that's the whole point – Pointy Feb 04 '21 at 02:30
  • 1
    actually, they *should* return a Promise; an `await` expression *always* results in a Promise. – Pointy Feb 04 '21 at 02:31
  • Does this answer your question? [Javascript async function console log the returned data](https://stackoverflow.com/questions/48036038/javascript-async-function-console-log-the-returned-data) – Chayim Friedman Feb 04 '21 at 02:31
  • You need to place the `console.log` *inside* the `returnAsync` function, and log `x`. – Bergi Sep 09 '21 at 14:18

3 Answers3

1

async functions must return a promise. (they implicitly return Promise<void> instead of void!)

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

For example, the following:

async function foo() {
   return 1
}

...is equivalent to:

function foo() {
   return Promise.resolve(1)
}

Source

0xLogN
  • 3,289
  • 1
  • 14
  • 35
0

//const getWeather = async () => {...

An async function's return value will be a Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function.

check more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ravi
  • 11
  • 3
0

This could be to do with the fact that you're trying to call an async function inside of a synchronous function. It's quite a common mistake to make, so don't fret. Typically if you think about the structure, the console logs could technically be run before the async function has completed. That's why we have "callback functions" which basically just run as soon as the async returns a value.

Axios has a really neat way to use these callbacks which is just using the .then() function.

So try this out and see if it works:

const axios = require('axios');
const getWeather = async () => {
    axios.get('http://api.openweathermap.org/data/2.5/forecast?id=524901&appid={apiKey}')
    .then(json => console.log(json))
    .catch(error => console.log(error))
};

getWeather();

Because you're trying to call the get request inside of an async function, you cannot then grab the data in a synchronous function.

Joshua Boddy
  • 83
  • 1
  • 9
  • I see, but how can I store the data from an API in another variable? I am going to have to use this data in another part of the program. – Johnny Vazquez Feb 04 '21 at 02:56
  • So the rule is that you can run synchronous functions inside of asynchronous functions, but not vice versa. So typically you want to orient your program around the async functions, it makes life a lot easier. So if you have another function to use the data with, I would run that function from inside the async function callback (the `.then()` bit) which will only run once the data is retrieved – Joshua Boddy Feb 04 '21 at 03:39