Angular 在调用函数之前,如何等待一个承诺中的两个承诺得到解决?
我在matches fetch上有一个forEach,它可以执行以下操作:Angular 在调用函数之前,如何等待一个承诺中的两个承诺得到解决?,angular,typescript,ecmascript-6,es6-promise,Angular,Typescript,Ecmascript 6,Es6 Promise,我在matches fetch上有一个forEach,它可以执行以下操作: matches => { matches.forEach(match => { Promise.all([this.teamService.getTeam(match._links.homeTeam.href)]) .then( team => { match.homeTea
matches => {
matches.forEach(match => {
Promise.all([this.teamService.getTeam(match._links.homeTeam.href)])
.then( team => {
match.homeTeam = team[0].teamName;
}
);
Promise.all([this.teamService.getTeam(match._links.awayTeam.href)])
.then( team => {
match.awayTeam = team[0].teamName;
}
);
this.updateTableInformation(match);
});
return matches;
}
解释:我带来了一系列的比赛,并对每一场比赛进行了检查。每场比赛都是一个承诺,包含主客场球队的链接
这些match.home和match.away值也是团队的承诺,因此我将团队包装为承诺。所有这些都是在将值分配给字符串类型值之前解决的:match.homeTeam和match.awayTeam
问题:
当我调用函数时:
this.updateTableInformation(match);
它使用match.homeTeam和match.awayTeam,但当它到达那里时,团队的承诺尚未解决,因此match.homeTeam=未定义
问题:
在调用updateTableInformation(match)之前,如何等待团队承诺(以及更高级别的匹配承诺)得到解决
我用的是es6,es2016
我用一个承诺来包装球队。所有这些都是在给比赛赋值之前解决的
不,仅仅是链接。然后(…)
直接链接到承诺就足够了
当它到达函数this.updateTableInformation(match)时,团队的承诺尚未得到解决
这就是您应该使用的Promise.all
,等待您需要等待的所有承诺,并在中使用它们的结果,然后
回调返回的承诺:
function updateMatch(match) {
const homePromise = this.teamService.getTeam(match._links.homeTeam.href).then(team => {
match.homeTeam = team[0].teamName;
});
const awayPromise = this.teamService.getTeam(match._links.awayTeam.href).then(team => {
match.awayTeam = team[0].teamName;
});
return Promise.all([homePromise, awayPromise]).then(() => {
this.updateTableInformation(match);
});
}
还可以使用Promise.all
等待迭代数组中的所有匹配完成。不要使用.forEach
,使用.map
,这样您就可以得到一系列要使用的承诺:
matches => Promise.all(matches.map(updateMatch))
我用一个承诺来包装球队。所有这些都是在给比赛赋值之前解决的
不,仅仅是链接。然后(…)
直接链接到承诺就足够了
当它到达函数this.updateTableInformation(match)时,团队的承诺尚未得到解决
这就是您应该使用的Promise.all
,等待您需要等待的所有承诺,并在中使用它们的结果,然后
回调返回的承诺:
function updateMatch(match) {
const homePromise = this.teamService.getTeam(match._links.homeTeam.href).then(team => {
match.homeTeam = team[0].teamName;
});
const awayPromise = this.teamService.getTeam(match._links.awayTeam.href).then(team => {
match.awayTeam = team[0].teamName;
});
return Promise.all([homePromise, awayPromise]).then(() => {
this.updateTableInformation(match);
});
}
还可以使用Promise.all
等待迭代数组中的所有匹配完成。不要使用.forEach
,使用.map
,这样您就可以得到一系列要使用的承诺:
matches => Promise.all(matches.map(updateMatch))
为什么要调用
Promise.all
在一个数组中使用单个Promise是的,看起来您需要将teamService.getTeam()
调用组合在同一个Promise.all
array/call中。@Bergi老实说,以我有限的知识,我使用了Promise.all,因为我知道它是如何等待解决的,而只包装一个元素是为了确保顺序。我在每场比赛中都有两个团队,然后是团队[0],团队[1],但有时值会颠倒。请注意,这一承诺并不妨碍JavaScript在函数的其余部分继续运行。只有“thens”中的代码正在等待。建议您使用更精确的语言。你不能有“承诺中的承诺”。事实上,除了一些内部状态之外,没有任何东西是“在一个承诺内”的。为什么要在一个只有一个承诺的数组上调用promise.all
是的,看起来您需要将teamService.getTeam()
调用组合在同一个Promise.all
array/call中。@Bergi老实说,以我有限的知识,我使用了Promise.all,因为我知道它是如何等待解决的,而只包装一个元素是为了确保顺序。我在每场比赛中都有两个团队,然后是团队[0],团队[1],但有时值会颠倒。请注意,这一承诺并不妨碍JavaScript在函数的其余部分继续运行。只有“thens”中的代码正在等待。建议您使用更精确的语言。你不能有“承诺中的承诺”。事实上,除了一些内在的状态之外,没有任何东西是“在一个承诺之内”的。谢谢你,这就成功了!我从这个问题中学到了比我期望的更多的东西,呵呵。谢谢谢谢你,这就成功了!我从这个问题中学到了比我期望的更多的东西,呵呵。谢谢