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);