Node JS Promise.all() 结果命名

Node JS Promise.all() results naming

有没有办法为 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);
});

可以在.then回调函数中使用ES2015的Array destructuring

Promise.all([
    getBalls, 
    getKids, 
    getTeams
]).then(function ([ balls, kids, teams ]) { // <= notice the function parameters
    const new_team = doSomething(balls, kids, teams);
    resolve(new_team);
});

在最近的 node.js ( > node v6.x) 版本中,您可以使用解构并执行

Promise.all([getBalls, getKids, getTeams])
.then(function ([balls, kids, teams]) {
    const new_team = doSomething(results[0], results[1],results[2])
    resolve(new_team);
});

也许 Promise.props 看起来更容易处理。 http://bluebirdjs.com/docs/api/promise.props.html

 Promise.props({
   balls: getBalls,
   kids: getKids,
   teams: getTeams
 })
 .then((results) => {
   const new_team = doSomething(results.balls, results.kids, results.teams)
   resolve(new_team);
 });