Skip to content

Instantly share code, notes, and snippets.

@mightyguava
Last active March 15, 2017 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mightyguava/aee3ad3218cfc3d10f72e74446956b4b to your computer and use it in GitHub Desktop.
Save mightyguava/aee3ad3218cfc3d10f72e74446956b4b to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - fetchJson promise closurized
// 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