riz*_*oro 6 javascript asynchronous promise q bluebird
我是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
Run Code Online (Sandbox Code Playgroud)
这是我的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;
Run Code Online (Sandbox Code Playgroud)
}
在这种情况下如何为"compare"方法分配多个值?我错过了承诺的意义吗?
Run Code Online (Sandbox Code Playgroud).then(compare()) <--- here is the problem
该then
方法确实需要一个返回另一个promise [或普通值]的函数,因此您需要在compare
不调用它的情况下传递.如果需要指定参数,请使用包装函数表达式:
User.findByEmail(email)
.then(function(user) {
return compare(/* magic */);
}).…
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2356 次 |
最近记录: |