Skip to content

Instantly share code, notes, and snippets.

@Chudesnov
Created December 1, 2017 12:08
Show Gist options
  • Save Chudesnov/1633ee4cbadc23468db948ca2e492461 to your computer and use it in GitHub Desktop.
Save Chudesnov/1633ee4cbadc23468db948ca2e492461 to your computer and use it in GitHub Desktop.
Async / Await
function renderUserProfile(userId) {
return fetch('/api/user').then(response => response.json()).then(user => {
const {
firstName,
lastName,
links
} = user;
const result = {
fullName: `${firstName} ${lastName}`
}
const linksPromises = links.map(({ social_network, id }) =>
getSocialNetworkUrl(social_network, id)
);
return Promise.all(linksPromises).then(links => {
return {
...result,
links
}
})
})
}
function renderUserPage() {
context.set('renderUserPage');
return renderUserProfile(userId)
.then(() => renderUserPosts(userId))
.then(() => context.unset('renderUserPage'))
.catch(err => someResolver())
}
async function renderUserPage() {
try {
await renderUserProfile(userId);
} catch (error) {
error.stack
}
await renderUserPosts(userId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment