[英]Await inside an asynchronous function not waiting in Node.js/express
我正在建立(或模拟)允许英雄联盟玩家查看并比较自己和其他玩家的统计信息的网站。 但是,我有一个问题,在完成数据检索之前将加载下一页。
我尝试将所有函数放在异步函数中,然后再调用该函数。 我也曾尝试建立Promises,但我一直做错事并使自己感到困惑。
例如,我的两个主要功能是
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));
})
}
和
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;
}
然后我打电话给他们
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";
}
然后在这里调用:
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');
})
});
在console.log(summoner)之后,retrieveData中的变量的console.logs都是空的,但是数据在“ 5s之后”部分中,所以我知道我的函数正在工作。 我很困惑和迷路,很抱歉,冗长的帖子。
尝试使getHighestMastery
函数异步
async function getHighestMastery(summonerRegion, summonerID)
并等待api.get()
调用。
var data = await api.get(summonerRegion, 'championMastery.getAllChampionMasteries', summonerID)
另外,尝试添加
.catch(error => {
console.log(error));
reject(error);
}
到getSummonerID
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.