I'm trying to get data from an API request but I don't know why await does not wait to get data to return value. Anyone can help me?
async function getChampionMasteryData(championId, summonerName) {
try {
var summonerId = process.env.SUMMONER_ID;
let championMasteryData = await axios(`https://euw1.api.riotgames.com/lol/champion-mastery/v4/champion-masteries/by-summoner/${summonerId}/by-champion/${championId}?api_key=${process.env.API_KEY}`, {responseType: 'json'});
return championMasteryData.data;
} catch (e) {
console.log(e);
}
}
Output:
Promise { <pending> }
It's quite likely that you are not awaiting for the getChampionMasteryData function. You should call it like await getChampionMasteryData(championId, summonerName) instead of calling it directly.
If you want to call it from a non-async function, you can use then to wait until it resolves like below,
getChampionMasteryData(championId, summonerName).then((data) => {
console.log(data);
});