所以我最近开始学习承诺(蓝鸟),现在我尽量使用它们,但是如果我需要在这种情况下返回承诺,我有点困惑。对承诺感到困惑。我需要回到这里吗?
这里我有一个Passport LocalStrategy,我提出:
passport.use(new LocalStrategy(function(username, password, done) {
users.get(username) // A
.then(function(user) {
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
bcrypt.compare(password, user.password).then(function(result) { // B
if (result) {
return done(null, user);
}
return done(null, false, { message: 'Incorrect password.' });
});
})
.catch(function(err) {
return done(err);
});
}));
users.get(username)
A线使用pg-promise库返回如果在数据库中找到并为假是否会分解成一个用户一个承诺用户没有找到。
bcrypt.compare
在线B使用bcrypt来检查密码和哈希匹配。它返回一个将解析为真或假的承诺。
代码工作完美,我只是困惑,如果线A和B应该返回像这样
return users.get(username) // A
return bcrypt.compare(password, user.password).then(function(result) { // B
的代码和不返回的承诺工作。
Passport/Node是否等到看到return done
?这是否意味着这个函数是同步的,即使它里面的所有东西都是异步的?通常你会返回一个承诺,然后使用.then()
就可以了,但由于LocalStrategy没有使用.then()
或.catch()
我不需要返回任何东西?任何输入是不胜感激。谢谢。
至少相关:http://stackoverflow.com/questions/23920589/how-to-pass-a-third-argument-to-a- callback-using-bluebird-js-nodeify – Tomalak
我发现这是一篇非常好的文章,解释了承诺的混乱[我们遇到了承诺问题](https://pouchdb.com/2015/05/18/we-have- a-problem-with-promises.html) – ivn
Tomalak,ivn:谢谢,我会检查一下。 – Nikokin