passport.js和process.nextTick在策略中 [英] passport.js and process.nextTick in strategy

查看:93
本文介绍了passport.js和process.nextTick在策略中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nodeJS中面临新问题: process.nextTick

I'm facing something new in nodeJS: process.nextTick

在某些策略中,护照的代码示例.js,我们可以看到

In some strategies code examples for passport.js, we can see

passport.use(new LocalStrategy(
  function (username, password, done) {

    // asynchronous verification, for effect...
    process.nextTick(function () {

      findByUsername(username, function (err, user) {
        // ...
        bcrypt.compare(password, user.password, function (err, res) {
          // ...
        });
      })
    });
  }
));

但在官方文档中,没有使用。 ( http://passportjs.org/guide/username-password/

But in the official documentation, it is not used. (http://passportjs.org/guide/username-password/)

我所理解的是,应该使用 process.nextTick 来延迟同步堆栈以阻止事件。但是在这个策略代码中,没有事件。

What I understand is that process.nextTick should be used to defer synchronous stack to not block an event. But in this strategy code, there is no event.

这里有什么好处?

推荐答案

在示例中只显示异步认证是可能的。在大多数情况下,您会查询数据库,因此它本质上是异步的。然而,这个例子只是一个硬编码的用户组,所以 nextTick 调用是有效的,以模拟异步函数。

It's only there in the example to show that async authentication is possible. In most cases, you'd be querying a database, so it'd be async in nature. However, the example just has a hard-coded set of users, so the nextTick call is there for effect, to simulate an async function.

这篇关于passport.js和process.nextTick在策略中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆