Using ES6 array destructing with Promises

Manuel Romero on November 09, 2017

(originally posted more than one year ago on Medium) As you know, some months ago, it was announced the new features of ES6 or EcmaScript2015. I... [Read Full]
markdown guide
 

ES6 allows the reverse as well which means the method above can be even neater, Also since the entire method is marked async there is no need for the try/catch if your not interested in the error (as in the example above).

async function getAllUserData() {
  let [ userInfo, userFriends, userSkills ] = await Promise.all([
    getUserInfo(),
    countUserFriends(),
    getUserSkills()
  ];
  return {userInfo, userFriends, userSkills};
}

Some Promise libraries offer a hash function for this purpose:

async function getAllUserData() {
  return RSVP.hash({
    userInfo: getUserInfo(),
    userFriends: countUserFriends(),
    userSkills: getUserSkills()
  });
}
 

It does not make sense to create a rejectable Promise if it cannot be rejectable. So you can use directly:

return Promise.resolve("whatever");

//instead of

return new Promise((resolve, reject)=> {
 resolve("whatever");
});
code of conduct - report abuse