Skip to content

Instantly share code, notes, and snippets.

@mightyguava
Created March 12, 2017 18:43
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/bcc4d278bf88082e0a7aa1e477d9ca85 to your computer and use it in GitHub Desktop.
Save mightyguava/bcc4d278bf88082e0a7aa1e477d9ca85 to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - fetchJson promise fixed
fetchJson('/api/user/self')
.then(function (user) {
return fetchJson('/api/user/interests?userId=' + self.id)
.then(interests => {
user: user,
interests: interests
});
})
.then(function (blob) {
return Promise.all[blob.interests.map(i => fetchJson('/api/recommendations?topic=' + i))]
.then(recommendations => {
user: blob.user,
interests: blob.interests,
recommendations: recommendations
});
})
.then(function (bigBlob) {
render(bigBlob.user, bigBlob.interests, bigBlob.recommendations);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment