Created
December 1, 2017 11:53
-
-
Save Chudesnov/8b3e668eb7c31a5ce4b2a460bc088d02 to your computer and use it in GitHub Desktop.
Promises
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
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