node.js - 在 Node 的 async 模块中使用函数的参数

标签 node.js asynchronous callback

我学习 Node.js 并在 MDN 的指导下做了一个小网站.

我使用模块 async 到达了那里.
这是代码

async.parallel({
    book_count: function(callback) {
        Book.countDocuments({}, callback); // Pass an empty object as match condition to find all documents of this collection
    },
    book_instance_count: function(callback) {
        BookInstance.countDocuments({}, callback);
    },
    book_instance_available_count: function(callback) {
        BookInstance.countDocuments({status:'Available'}, callback);
    },
    author_count: function(callback) {
        Author.countDocuments({}, callback);
    },
    genre_count: function(callback) {
        Genre.countDocuments({}, callback);
    }
}, function(err, results) {
    res.render('index', { title: 'Local Library Home', error: err, data: results });
});

};

此代码计算数据库中的文档数。 我不明白什么是参数“回调”。这些属性“book_count、book_instance_count、author_count 等”中没有发送任何内容,但所有这些功能都运行良好。 请向我解释一下,什么是奇怪的参数“回调”以及如何使用它。

最佳答案

回调函数

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);

https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

关于node.js - 在 Node 的 async 模块中使用函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458625/

相关文章:

node.js - cron 作业可以每 'x' 秒运行一次吗

node.js - Sequelize-typescript 包含多对多仅包含多个结果之一

python - 从另一个线程调度异步协程,无需大量回调和同步等待

jquery - 处理 jQuery Treetable 中的回调

node.js - TypeScript Node.js 该命令未定义

javascript - Rails - 找不到 JavaScript 运行时?

javascript - 如何循环firebase的异步查询代码?

c# - 异步等待请求限制

Jquery 在用户定义的函数之间等待

android - 将回调从 Activity 发送到 Fragment