index.js:
console.log('Before');
getUser(1, getRepositories);
console.log('After');
function getRepositories(user) {
getRepositories(user.gitHubUsername, getCommits);
}
function getCommits(repos) {
getCommits(repo, displayCommits);
}
function displayCommits(commits) {
console.log(commits);
}
function getCommits(repo,callback) {
setTimeout(() => {
console.log('Getting commits for a GitHub repo...');
callback(['commit1', 'commit2', 'commit3']);
}, 2000)
}
function getUser(id, callback) {
setTimeout(() => {
console.log('Reading a user from a database...');
callback({ id: id, gitHubUsername: 'abc' });
}, 2000)
}
function getRepositories(username, callback) {
setTimeout(() => {
console.log('Calling GitHub API...');
callback(['repo1', 'repo2', 'repo3']);
}, 2000);
}
我导航到包含该文件的路径并执行命令:node index.js并得到错误:回调不是函数。
有人可以在这里指导我解决这个问题吗?
答案 0 :(得分:0)
以下是修补程序的更新代码:
index.js:
console.log('Before');
getUser(1, getRepositorie);
console.log('After');
function getRepositorie(user) {
getRepositories(user.gitHubUsername, getCommit);
}
function getCommit(repos) {
getCommits(repos[0], displayCommits);
}
function displayCommits(commits) {
console.log(commits);
}
function getCommits(repo,callback) {
setTimeout(() => {
console.log('Getting commits for a GitHub repo...');
callback(['commit1', 'commit2', 'commit3']);
}, 2000)
}
function getUser(id, callback) {
setTimeout(() => {
console.log('Reading a user from a database...');
callback({ id: id, gitHubUsername: 'abc' });
}, 2000)
}
function getRepositories(username, callback) {
setTimeout(() => {
console.log('Calling GitHub API...');
callback(['repo1', 'repo2', 'repo3']);
}, 2000);
}