Search code examples
javascriptnode.jsbluebirdnaming

Node JS Promise.all() results naming


Is there a way for assigning a name for each result in the Promisee.all?

Let's say this is my code:

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

And I want it to be something like:

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

Solution

  • You can use ES2015's Array destructuring in the .then callback function:

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