Named Functions To The Rescue

In the 'Named Functions To The Rescue Section" In “Asynchronous JavaScript” can anybody get the exercise to work?
When my code gets to the getRepositories() function, I get an error saying the callback is not a function and I cannot figure out why.

Thanks for any help.

I’ve found out that the problem emerges from the function names:

  • getCommits(repos) & getCommits(repos, callback)

  • getRepositories(user) & getRepositories(username, callback)

Due to their different params, they did not seem to cause the error. However, when I just tried to have a unique name for each function, it turned out to work fine. :smiley: (my node version is v14.15.4)

Below is the way I’ve adjusted the code:

console.log(‘Before’);

getUser(1, readRepositories);

console.log(‘After’);

function readRepositories(user) {
getRepositories(user.gitHubUsername, readCommits);
}

function readCommits(repos) {
getCommits(repos, displayReposCommits);
}

function displayReposCommits(commits) {
console.log(commits);
}

function getUser(id, callback) {
setTimeout(()=> {
console.log(‘Reading a user from a DB…’);
callback({id: id, gitHubUsername: ‘Gioia’});
}, 2000);

}

function getRepositories(username, callback) {
setTimeout(()=> {
console.log(‘Calling GitHub API…’);
console.log(callback);
callback({
username: username,
repositories: [‘repo1’, ‘repo2’, ‘repo3’]
});
}, 2000);

}

function getCommits(repos, callback) {
setTimeout(() => {
console.log(Reading commits ...);
console.log(callback);
callback({
repository: repos,
commits: [‘1st commit’, ‘2nd commit’]
});
}, 8000);
}

2 Likes

Ran into the same issue. Spent an hour troubleshooting. Thanks for sharing the solution. Renamed the callback functions like you did and everything works. Thank you!

I know this is an old post, but I recently ran into the same problem. I ended up renaming the functions as well. That’s great that I got it to work, but what happened here? Mosh said that you should be able to have functions with the same name as long as the parameters aren’t the same. I guess, maybe they changed this with a later version of node js or javascript?