I'm making (or mocking up) a website that allows League of Legends players to look up and compare stats of themselves and other players. However, I have an issue where the next page loads before the data is finished being retrieved.
I have tried placing all my functions in an async function, and calling that function later. I have also tried to set up Promises but I keep doing something wrong and getting myself confused.
For example, 2 of my main functions are
function getSummonerID(summonerRegion, summonerName) {
return new Promise((resolve, reject) => {
var dataSummoner = {};
api.get(summonerRegion, 'summoner.getBySummonerName', summonerName)
.then((data) => {
if (data) {
dataSummoner.id = data.id;
dataSummoner.accountId = data.accountId;
dataSummoner.name = data.name;
dataSummoner.profileIconId = data.profileIconId;
dataSummoner.summonerLevel = data.summonerLevel;
dataSummoner.exists = true;
dataSummoner.region = summonerRegion;
console.log("\ngetSummonerID complete and exists\n");
} else {
dataSummoner.exists = false;
console.log("\ngetSummonerID complete and doesnt exist\n");
}
resolve(dataSummoner);
})
.catch(error => console.log(error));
})
}
and
function getHighestMastery(summonerRegion, summonerID) {
var dataMastery = {};
api.get(summonerRegion, 'championMastery.getAllChampionMasteries', summonerID)
.then((data) => {
dataMastery.championId = data[0].championId;
dataMastery.championLevel = data[0].championLevel;
dataMastery.championPoints = data[0].championPoints;
for (var i = 0; i < Object.keys(championJSON.data).length; i++)
if ((dataMastery.championId) === (championJSON.data[Object.keys(championJSON.data)[i]].id)) {
dataMastery.championName = championJSON.data[Object.keys(championJSON.data)[i]].name;
dataMastery.championTitle = championJSON.data[Object.keys(championJSON.data)[i]].title;
}
})
.catch(error => console.log(error));
console.log("\ngetHighestMastery complete\n");
return dataMastery;
}
I'm then calling them
async function retreiveData(summonerRegion, summonerName) {
summoner = await getSummonerID(summonerRegion, summonerName);
summoner.name = summonerName;
summoner.region = summonerRegion;
console.log("summoner");
console.log(summoner);
if (summoner.exists) {
mastery = await getHighestMastery(summonerRegion, summoner.id);
console.log("mastery");
console.log(mastery);
matches = await getRecentGames(summonerRegion, summoner.accountId, 10);
console.log("matches");
console.log(matches);
rankedInfo = await getRankedInfo(summonerRegion, summoner.id);
console.log("rankedInfo");
console.log(rankedInfo);
console.log("calls done");
}
return "done";
}
and in turn, calling that here:
router.post('/summoner/submit', function(req, res, next) {
summoner.region = req.body.summRegion;
summoner.name = req.body.summName;
if (summoner.name) {
title = summoner.name + " on " + summoner.region + " - LOLSTATS.GG";
}
var x = retreiveData(summoner.region, summoner.name);
console.log("x is:");
console.log(x);
x.then((testVar) => {
console.log("test var: " + testVar);
setTimeout(function() {
console.log("summoner after 5s");
console.log(summoner);
console.log("mastery after 5s");
console.log(mastery);
console.log("matches after 5s");
console.log(matches);
console.log("rankedInfo after 5s");
console.log(rankedInfo);
}, 5000);
res.redirect('/summoner/lookup');
})
});
the console.logs of variables in retrieveData AFTER console.log(summoner) are all empty, but the data is there in the "after 5s" part, so I know my functions are working. I'm so confused and lost, sorry for the long post.