Last active
December 16, 2020 02:19
-
-
Save yuhanx0728/0fed65ed53e209d54ff2f831a71b566b to your computer and use it in GitHub Desktop.
Use map() with Promise - Promise.all(); also notice the use of Object.assign() here
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
| // https://www.smashingmagazine.com/2018/01/graphql-primer-new-api-part-1/ | |
| const getPostWithAuthor = postId => { | |
| return getPost(postId) | |
| .then(post => getAuthor(post.author)) | |
| .then(author => { | |
| return Object.assign({}, post, { author }) | |
| }) | |
| }; | |
| const getHomePageData = () => { | |
| return getPosts() | |
| .then(postIds => { | |
| const postDetails = postIds.map(getPostWithAuthor); | |
| return Promise.all(postDetails); | |
| }) | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment