DEV Community

Discussion on: Using ES6 array destructing with Promises

Collapse
 
sukima profile image
Devin Weaver

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()
  });
}