hi everybody for those of you who struggled about the “named function to avoid callback hell” exemple provided by Mosh in his course I have found the solution.
In the exemple he provides he give the same name to 2 different function:
getRepositories and getCommits. I have spent a good amount of time and I just found the fix; give the functions diffente names. In my case I have added a number to the named function.
Have a look at my code bellow,
console.log(‘Before’);
getUser(1, getRepositories1);
console.log(‘After’);
function getRepositories1(user){
getRepositories(user.gitHubUsername, getCommits1);
}
function getCommits1(repos) {
getCommits(repos,displayCommits);
}
function displayCommits(commits){
console.log(commits);
}
function getUser(id, callback){
setTimeout(() =>{
console.log(‘Reading db…’);
callback({id: id, gitHubUsername: ‘mosh’});
}, 2000);
}
function getRepositories(username, callback){
setTimeout(() => {
console.log(‘Reading database…’);
callback( [‘repo1’, ‘repo2’, ‘repo3’]);
}, 2000);
}
function getCommits(repo, callback){
setTimeout(() => {
console.log(‘Reading DB to display all commits…’);
callback([‘commit1’,‘commit2’,‘commit3’]);
}, 2000);
}