有没有办法为 Promisee.all 中的每个结果分配名称?
假设这是我的代码:
Promise.all([getBalls, getKids, getTeams])
.then(function (results) {
const new_team = doSomething(results[0], results[1],results[2])
resolve(new_team);
});
我希望它是这样的:
Promise.all([
balls: getBalls,
kids: getKids,
teams: getTeams
]).then(function (results) {
const new_team = doSomething(balls,kids,teams)
resolve(new_team);
});
最佳答案
可以使用ES2015的Array destructuring在 .then
回调函数中:
Promise.all([
getBalls,
getKids,
getTeams
]).then(function ([ balls, kids, teams ]) { // <= notice the function parameters
const new_team = doSomething(balls, kids, teams);
resolve(new_team);
});
关于javascript - Node JS Promise.all() 结果命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43224546/