Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Last active January 18, 2018 19:51
Show Gist options
  • Select an option

  • Save ktilcu/e1ff43924ca304225fe4d651175244a2 to your computer and use it in GitHub Desktop.

Select an option

Save ktilcu/e1ff43924ca304225fe4d651175244a2 to your computer and use it in GitHub Desktop.
Alternative Promise code
/** Utility Functions **/
// Calls a method on an object
const call = method => value => value[method]();
/** Business Logic **/
const getUserAndFriendsAvatars = async user => {
// get user
const user = getUser(user);
// get users friends
const friends = user.then(call('friends'));
//get users avatar
const avatar = user.then(call('avatar'));
// get the friends avatars
const friendAvs = Promise.all(friends.then(fs => fs.map(call('avatar'))));
// wait for everything to finish
return Promise.join(avatar, friendAvs).then((a, as) => a.concat(as));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment