[英]Bluebird promisify multiple arguments
我是Promises的新手,不知道如何解决这个问题:我正在做一个auth系统,我的第一个电话是检查数据库上的电子邮件。 如果用户存在,那么根据加密的密码检查密码...我正在使用这个lib进行bcrypt: https ://npmjs.org/package/bcrypt这不是promises兼容的,所以我使用的是“promisify”for以下签名:compare(密码,crypted_password,回调)。
所以这是我的代码:
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;
}
在这种情况下如何为“compare”方法分配多个值? 我错过了承诺的意义吗?
.then(compare()) <--- here is the problem
then
方法确实期望一个返回另一个promise [或普通值]的函数,所以你需要在不调用它的情况下传递compare
。 如果需要指定参数,请使用包装函数表达式:
User.findByEmail(email)
.then(function(user) {
return compare(/* magic */);
}).…
我做了Bergi所说的并且适合我的事情:
this.findByEmail(email)
.then(function(user) {
return compare(password, user.password);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.