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. (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);
}