回调不是使用 nodejs 进行异步操作的函数

桑托什·库马尔·帕特罗

索引.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 并得到错误:回调不是函数。

任何人都可以在这里指导我解决这个问题吗?

桑托什·库马尔·帕特罗

这是带有修复程序的更新代码:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章