我学习 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/