Skip to content

Instantly share code, notes, and snippets.

@Chudesnov
Created December 1, 2017 11:53
Show Gist options
  • Save Chudesnov/8b3e668eb7c31a5ce4b2a460bc088d02 to your computer and use it in GitHub Desktop.
Save Chudesnov/8b3e668eb7c31a5ce4b2a460bc088d02 to your computer and use it in GitHub Desktop.
Promises
const ourPromise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Hello'), 1000)
});
ourPromise
.then(resolvedValue => {
return resolvedValue;
})
.then(anotherValue => {
return new Promise((resolve, reject) => {
Math.random() > 0.5 ?
setTimeout(() => resolve('Hello'), 10000) :
setTimeout(() => reject(new Error('Bye')), 10000)
})
})
new Promise(resolve => {
document.addEventListener('click', resolve);
})
const request = fetch('/api/user').then(response => response.json());
renderWhenLoaded(request);
function renderWhenLoaded(request) {
request.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
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment