Domanda

I'm new to Promises, and don't know how to resolve this problem: I'm doing an auth system, and my first call is to check email on database. If user exists, then check password against a bcrypted password... I'm using this lib for bcrypt: https://npmjs.org/package/bcrypt which is not promises compatible, so I'm using the "promisify" for the following signature: compare(password, crypted_password, callback).

So this is my code:

var compare = Promise.promisify(bcrypt.compare);

User.findByEmail(email)   
    .then(compare()) <--- here is the problem

This is my findByEmail method:

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;

}

How to assign multiple values to the "compare" method in that case? Am I missing the point of promises?

È stato utile?

Soluzione

.then(compare()) <--- here is the problem

The then method does expect a function which returns another promise [or a plain value], so you would need to pass compare without calling it. If you need to specify the arguments, use a wrapper function expression:

User.findByEmail(email)   
    .then(function(user) {
         return compare(/* magic */);
    }).…

Altri suggerimenti

I did exactly what Bergi said and works for me:

this.findByEmail(email)
.then(function(user) {
  return compare(password, user.password);
})
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top