我是 Promises 的新手,不知道如何解决这个问题: 我正在做一个身份验证系统,我的第一个电话是检查数据库中的电子邮件。如果用户存在,则根据 bcrypted 密码检查密码...我将此库用于 bcrypt:https://npmjs.org/package/bcrypt这与 promises 不兼容,所以我对以下签名使用“promisify”:compare(password, crypted_password, callback)。
所以这是我的代码:
var compare = Promise.promisify(bcrypt.compare);
User.findByEmail(email)
.then(compare()) <--- here is the problem
这是我的 findByEmail 方法:
User.prototype.findByEmail = function(email) {
var resolver = Promise.pending();
knex('users')
.where({'email': email})
.select()
.then(function(user) {
if (_.isEmpty(user)) { resolver.reject('User not found'); }
resolver.fulfill(user);
});
return resolver.promise;
在这种情况下如何将多个值分配给“比较”方法?我错过了 promise 的重点吗?
最佳答案
.then(compare()) <--- here is the problem
then
method确实期望一个返回另一个 promise [或一个普通值] 的函数,因此您需要传递 compare
而不调用它。如果需要指定参数,请使用包装函数表达式:
User.findByEmail(email)
.then(function(user) {
return compare(/* magic */);
}).…
关于javascript - Bluebird promise 多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21271400/