Skip to content

Instantly share code, notes, and snippets.

@yuhanx0728
Last active December 16, 2020 02:19
Show Gist options
  • Select an option

  • Save yuhanx0728/0fed65ed53e209d54ff2f831a71b566b to your computer and use it in GitHub Desktop.

Select an option

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
// 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