好的,我真的在这里做什么,以确保一切按顺序执行。感觉好像我错过了一个等待或其他非常简单的事情,但是我真的无法拼凑出为什么我的返回等待不只是在所有其他提取完成之后返回的原因。
async function generateData(region, user) {
const apiKey = "APIKEY";
let promises = [];
let gameData = [];
fetch("https://" + region + ".api.riotgames.com/lol/summoner/v4/summoners/by-name/" + user + "?api_key=" + apiKey)
.then(response => response.json())
.then(user => {
return fetch("https://" + region + ".api.riotgames.com/lol/match/v4/matchlists/by-account/" + user.accountId + "?api_key=" + apiKey)
})
.then(response => response.json())
.then(data => {
const matches = data.matches.slice(0, 10);
for (let match of matches) {
promises.push(fetch("https://" + region + ".api.riotgames.com/lol/match/v4/matches/" + match.gameId + "?api_key=" + apiKey).then(response => response.json()).then(game => {
return new Game(
game.teams[0].win,
"Win",
"Fail",
idFetch([game.participants[0].championId, game.participants[1].championId, game.participants[2].championId, game.participants[3].championId, game.participants[4].championId]),
idFetch([game.participants[5].championId, game.participants[6].championId, game.participants[7].championId, game.participants[8].championId, game.participants[9].championId])
);
}));
}
});
return await Promise.all(promises).then(dataArray => {
console.log(dataArray);
return dataArray;
});
}
在返回结果之前,解释器不会等待您的提取结束。尝试在第5行await
之前添加fetch...
。但是实际上很难读,不要将async / await语法与.then混合使用,那么对自己来说更容易理解。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句