Javascript NodeJS 中的“完成”属性是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28656780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:12:12  来源:igfitidea点击:

What is the attribute "done" in NodeJS?

javascriptnode.jspassport.js

提问by David Luque

I'm coding the local login in NodeJS following this tutorial:

我正在按照本教程在 NodeJS 中编写本地登录:

https://scotch.io/tutorials/easy-node-authentication-setup-and-local

https://scotch.io/tutorials/easy-node-authentication-setup-and-local

In the file config/passport.js

在文件 config/passport.js 中

function(req, email, password, done){
    process.nextTick(function(){
        User.findOne({'local.email' :   email}, function(err, user){
            if(err)
                return done(err);
            if (user){
                return done(null, false, req.flash('signupMessage', 'message'));
            }

I'm rookie in NodeJS and Javascript, and I don't understand how a value like "done" can be a function (return done(err)). Is any system function?

我是 NodeJS 和 Javascript 的新手,我不明白像“done”这样的值如何成为一个函数(返回 done(err))。有什么系统功能吗?

Thanks a lot!

非常感谢!

回答by Golo Roden

doneis a callback that you need to call once you are done with your work. As you can see it is given in the first line of your code:

done是完成工作后需要调用的回调。如您所见,它在代码的第一行中给出:

function(req, email, password, done){

This means that besides the incoming request you get the user-specified emailand password. Now you need to do whatever you need to do to verify the login. Somehow you need to tell Passport whether you succeeded or not.

这意味着除了传入请求之外,您还可以获得用户指定的emailpassword. 现在你需要做任何你需要做的事情来验证登录。无论您成功与否,您都需要以某种方式告诉 Passport。

Normally, you may use a return value for this, but in this case the Passport author thought about the option that your check may be asynchronous, hence using a return value would not work.

通常,您可以为此使用返回值,但在这种情况下,Passport 作者考虑了您的检查可能是异步的选项,因此使用返回值将不起作用。

This is why a callback is being used. Most often callbacks are being called callback, but this is just for convenience, there is no technical reason to do so. In this case, since the callback is being used for showing that you are done, the Passport author suggested to call it done.

这就是使用回调的原因。大多数情况下回调被调用callback,但这只是为了方便,没有技术原因这样做。在这种情况下,由于回调用于显示您已完成,因此 Passport 作者建议将其称为done

Now you can either call donewith an error if credential validation failed, or with the appropriate parameters to show that it succeeded.

现在,您可以done在凭据验证失败时使用错误进行调用,也可以使用适当的参数来表明验证成功。

This works because functions are so-called first-class citizensin JavaScript, i.e. there is no actual difference between code and data: In JavaScript you can pass functions around as parameters and return values as you can with data.

这是有效的,因为函数在 JavaScript 中是所谓的一等公民,即代码和数据之间没有实际区别:在 JavaScript 中,您可以将函数作为参数传递并返回值,就像使用数据一样。

And that's it :-)

就是这样:-)

回答by Quentin

In JavaScript, functions are first class objects.

在 JavaScript 中,函数是第一类对象。

They can be stored in variables and passed around like any other piece of data.

它们可以存储在变量中并像任何其他数据一样传递。

Function declarations create, in the current scope, a variable with the same name as the function.

函数声明在当前作用域内创建一个与函数同名的变量。

function call_done(done) {
    done();
}

function some_function () {
    alert("Ta da");
}

call_done(some_function);