0

I'm struggling to properly handle async/await. I did read through multiple tutorials, searched for similar questions on SO, read through the MDN reference... And I can't crack it. I am just trying to make a axios get request to openweathermap and grab temperature. And constantly get a Promise { } result. Hopefully there is only a small bit missing in my code (and my async/await understanding) and if someone could point me in the right direction, it would be awesome.

temp.js

const axios = require('axios');

const key = 'openweatherapikey'

const getData = async (id) => {
    try {
        return await axios.get('http://api.openweathermap.org/data/2.5/weather', {
            params: {
                id: id,
                units: 'metric',
                appid: key
            }
        });
    } catch (error) {
        console.log(error);
    }
};

const getCelsius = async (id) => {
/*1*/    const data = await getData(id);    
/*2*/    console.log(data.data.main.temp);  
/*3*/    data.data.main.temp;                

//when getCelsius(id) contains just line /*1*/ and is called in app.js, it outputs "Promise { <pending> }" in the console.
//when getCelsius(id) contains lines /*1*/ & /*2*/ and is called in app.js, it outputs "Promise { <pending> }" AND the temperature value in the console
//when getCelsius(id) contains lines /*1*/ & /*3*/ and is called in app.js, it outputs "Promise { <pending> }" in the console.
    
};

module.exports = {
    getCelsius
};

app.js

const { getCelsius } = require('./temp');

const output = getCelsius('someid')

console.log(output);
mintymike
  • 27
  • 8
  • hi, @VLAD, it does not. Nor did this one: https://stackoverflow.com/questions/34094806/return-from-a-promise-then. In the link you sent the selected answer does not give me much. It says that in another promise function I can use await to wait for the original promise to settle - which is what I thought I'm doing, yet I can't get the value. – mintymike Oct 21 '20 at 21:55
  • You're missing a `return` statement in `getCelsius`, that's why you can't get a value even if you wait for the promise to fulfill. – Bergi Oct 21 '20 at 22:06
  • 1
    @mintymike as Bergi said, you're missing a `return` in `getCelsius`. However, this just means that *eventually* the promise it produces resolves to `undefined`. You're not using `await getCelsius()` which is what will take the value out of the promise. However, to use that, you need [top level await](https://github.com/tc39/proposal-top-level-await) supported. Without that, you need to use the promise API `getCelsius().then(output => console.log(output))`. See also [How can I use async/await at the top level?](https://stackoverflow.com/q/46515764) – VLAZ Oct 22 '20 at 04:44
  • @VLAZ: Thanks for the patience, I know it's all noob stuff you're explaining. The return gave undefined, like you said. But I also used your suggestion with .then. Your example and all other examples show how to console.log the output from .then. It works. But I want the output to be assigned to a const - and that one is simply not clear in any of the examples I've seen. How do I assign the output of getCelsius('someid') to a const, say "temperature", which I can then use later in app.js? I suppose this could really help other beginners like myself... – mintymike Oct 22 '20 at 09:13
  • 1
    I'd point you again towards [How can I use async/await at the top level?](https://stackoverflow.com/q/46515764). The problem is that once a function is async, you cannot produce a "normal" result from it, only promises. And any function that relies on the first one has to also be async to handle the promises. It's async all the way down. To assign the extracted promise to a variable, you need to do `const temp = await getCelsius('someid')` but it can only be done in certain situations. The question I linked shows how to handle this at the top level of a module. – VLAZ Oct 22 '20 at 09:27
  • So in other words, if I want to use the value returned from the getCelsius(id) promise, all the code has to be contained in the scope of the async function? So: `(async () => {try { VALUE FROM PROMISE; USE VALUE FROM PROMISE ON OTHER OPERATIONS } catch {}}();`? Because simply trying to do `const temp = await getCelsius(id).then(something)` does not work, neither does `const temp = (async () => etc...`. – mintymike Oct 22 '20 at 12:41

0 Answers0