Last active
March 15, 2017 15:34
-
-
Save mightyguava/aee3ad3218cfc3d10f72e74446956b4b to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - fetchJson promise closurized
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
// We declare these variables we want to save ahead of time. | |
var user, recommendations; | |
fetchJson('/api/user/self') | |
.then(function (fetchedUser) { | |
user = fetchedUser; | |
return fetchJson('/api/user/interests?userId=' + self.id); | |
}) | |
.then(function (fetchedInterests) { | |
interests = fetchedInterests; | |
return Promise.all(interests.map(i => fetchJson('/api/recommendations?topic=' + i))); | |
}) | |
.then(function (recomendations) { | |
render(user, interests, recommendations); | |
}) | |
.then(function () { | |
console.log('We are done!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment