I'm trying to write chained-promises to perform multiple queries on the users ranking in various leagues, as well as gathering user data. But I don't understand how to join all the things i've gathered into a single coherent output.
This is the code I have so far:
const user = new User();
Users.getUserByName(name).then(function (foundUser) {
if (foundUser == null)
throw new Error("no user found");
// This is actually where the user is first found not the empty one above...
})
.then(Users.getUserTeamRank(name).then(function (teamRank) {
user.teamRank = teamRank;
console.log("TEAM_RANK " + teamRank);
}))
.then(Users.getUserSoloRank(name).then(function (soloRank) {
user.soloRank = soloRank;
console.log("SOLO_RANK " + soloRank);
}))
.then(Users.getUserFFARank(name).then(function (ffaRank) {
user.ffaRank = ffaRank;
console.log("FFA_RANK " + ffaRank);
}))
.catch(err => {
if (err.message === "no user found") {
msg.channel.send(MessageUtils.error("No player by the name {" + name + "} was found."));
return;
}
});
// these vars are undefined for examople
console.log(user.teamRank + " , " + user.ffaRank + ", " + user.soloRank);
The problem I'm having is that after all the promises have been executed the user.teamRank is undefined. How to proceed from here?