This is an example where we use Asynchronus Javascript to simulate retrieving the list of commits for a Github user's repositories.
getUser(1, (user)=>{
getRepositories(user.githubUsername, (repos)=>{
getCommits(repo , (commits)=>{
// This is callback hell\
});
});
});
function getCommits(repo){
return ['commit1','commit2'];
}
function getRepositories(username, callback){
setTimeout(()=>{
//Just for simulation
console.log('Retrieving repos');
return ['repo1','repo2'];
},2000);
function getUser(id,callback){
setTimeout(()=>{
console.log('Reading user from database');
callback({id: id, githubUsername: 'myName'});
},2000)
}
I want to print out the number of commits for each repository (just commit1 and commit2 for simplicity)... I know how to achieve this Synchronously... but is this implementation correct for doing it Asynchronously ? when getUser() is called... what executes after? the lambda function or the actual function?