回调错误不是 Timeout._onTimeout 处的函数
Error of callback is not a function at Timeout._onTimeout
代码:要回用的命名函数:
console.log('before');
getUser(1, getRepositories);
// console.log('User',user);
function getRepositories(user) {
// console.log(user);
getRepositories(user.gitHubUsername, getCommits);
}
function getCommits(repos) {
getCommits(repo, displayCommits);
}
function displayCommits(commits) {
console.log(commits);
}
console.log('after');
function getUser(id, callback) {
setTimeout(() => {
console.log('reading a user from databse...');
callback({ id: id, gitHubUsername: 'mosh'});
}, 2000);
}
function getRepositories(username, callback) {
setTimeout(() => {
console.log('calling GitHub API...');
callback(['repo1', 'repo2', 'repo3']);
},2000);
}
function getCommits(repo, callback) {
setTimeout(() => {
console.log('calling GitHub API...');
callback(['commit']);
},2000);
}
输出:
前
后
从数据库中读取用户...
呼叫 GitHub API...
C:\Users\jaini\node\async-demo\index.js:31
回调(['repo1','repo2','repo3']);
^
TypeError: callback is not a function
at Timeout._onTimeout (C:\Users\jaini\node\async-demo\index.js:31:5)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
在index.js的第31行,callback需要作为一个函数来调用(你看它不是橙色的,因为它还没有被声明)
正如您不希望 kids/pets/whatever 具有相同的名称一样,您不应该在同一范围内具有相同名称的 function
。那个可怜的 JS 引擎无法决定你想要 运行 哪个 getRepositories
或 getCommits
函数。尝试重命名这些,您就可以开始了:
console.log('before');
getUser(1, getRepositoriesByUser);
function getRepositoriesByUser(user) {
getRepositories(user.gitHubUsername, getCommitsByRepo);
}
function getCommitsByRepo(repo) {
getCommits(repo, displayCommits);
}
function displayCommits(commits) {
console.log(commits);
}
console.log('after');
function getUser(id, callback) {
setTimeout(() => {
console.log('reading a user from database...');
callback({ id, gitHubUsername: 'mosh' });
}, 2000);
}
function getRepositories(username, callback) {
setTimeout(() => {
console.log('calling GitHub API to get repos...');
callback(['repo 1', 'repo 2', 'repo 3']);
}, 2000);
}
function getCommits(repo, callback) {
setTimeout(() => {
console.log('calling GitHub API to get commits...');
callback(['commit A', 'commit B']);
}, 2000);
}
代码:要回用的命名函数:
console.log('before');
getUser(1, getRepositories);
// console.log('User',user);
function getRepositories(user) {
// console.log(user);
getRepositories(user.gitHubUsername, getCommits);
}
function getCommits(repos) {
getCommits(repo, displayCommits);
}
function displayCommits(commits) {
console.log(commits);
}
console.log('after');
function getUser(id, callback) {
setTimeout(() => {
console.log('reading a user from databse...');
callback({ id: id, gitHubUsername: 'mosh'});
}, 2000);
}
function getRepositories(username, callback) {
setTimeout(() => {
console.log('calling GitHub API...');
callback(['repo1', 'repo2', 'repo3']);
},2000);
}
function getCommits(repo, callback) {
setTimeout(() => {
console.log('calling GitHub API...');
callback(['commit']);
},2000);
}
输出: 前 后 从数据库中读取用户... 呼叫 GitHub API... C:\Users\jaini\node\async-demo\index.js:31 回调(['repo1','repo2','repo3']); ^
TypeError: callback is not a function
at Timeout._onTimeout (C:\Users\jaini\node\async-demo\index.js:31:5)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
在index.js的第31行,callback需要作为一个函数来调用(你看它不是橙色的,因为它还没有被声明)
正如您不希望 kids/pets/whatever 具有相同的名称一样,您不应该在同一范围内具有相同名称的 function
。那个可怜的 JS 引擎无法决定你想要 运行 哪个 getRepositories
或 getCommits
函数。尝试重命名这些,您就可以开始了:
console.log('before');
getUser(1, getRepositoriesByUser);
function getRepositoriesByUser(user) {
getRepositories(user.gitHubUsername, getCommitsByRepo);
}
function getCommitsByRepo(repo) {
getCommits(repo, displayCommits);
}
function displayCommits(commits) {
console.log(commits);
}
console.log('after');
function getUser(id, callback) {
setTimeout(() => {
console.log('reading a user from database...');
callback({ id, gitHubUsername: 'mosh' });
}, 2000);
}
function getRepositories(username, callback) {
setTimeout(() => {
console.log('calling GitHub API to get repos...');
callback(['repo 1', 'repo 2', 'repo 3']);
}, 2000);
}
function getCommits(repo, callback) {
setTimeout(() => {
console.log('calling GitHub API to get commits...');
callback(['commit A', 'commit B']);
}, 2000);
}