Created
March 12, 2017 18:42
-
-
Save mightyguava/750017bddc4c74b502f586621f37366f to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - fetchJson callbacks
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
// Suppose that we have a method fetchJson() that does GET requests and has an interface that looks | |
// like this, where callback is expected to take error as its first argument and the parsed response | |
// data as its second. | |
function fetchJson(url, callback) { ... } | |
fetchJson('/api/user/self', function(e, user) { | |
fetchJson('/api/interests?userId=' + user.id, function(e, interests) { | |
var recommendations = []; | |
interests.forEach(function () { | |
fetchJson('/api/recommendations?topic=' + interest, function(e, recommendation) { | |
recommendations.push(recommendation); | |
if (recommendations.length == interests.length) { | |
render(profile, interests, recommendations); | |
} | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment