Created
December 1, 2017 12:08
-
-
Save Chudesnov/1633ee4cbadc23468db948ca2e492461 to your computer and use it in GitHub Desktop.
Async / Await
This file contains 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
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