gpt4 book ai didi

javascript - Node JS Promise.all() 结果命名

转载 作者:太空宇宙 更新时间:2023-11-04 03:27:02 26 4
gpt4 key购买 nike

有没有办法为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com