Last active
January 18, 2018 19:51
-
-
Save ktilcu/e1ff43924ca304225fe4d651175244a2 to your computer and use it in GitHub Desktop.
Alternative Promise code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** 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