0

I have a function that fetch record from postgresql asyncronously. The issue i have been trying to solve is how I can return true or false if the record fetch is greater than 0.

Here is my function:

function checkUserValidity(email){
var result = false;     
client.connect(function(err){
    if(err) {
        client.end();
        return false;
    }
    client.query("SELECT * FROM users WHERE email='" + email + "'",function(err,data){

        if(err) {
            client.end();
            return false;
        }
        result = data.rowCount > 0;
        client.end();                   
    });
}); 

return result;
}

result is always false because of promise, how can i get this done?

  • 1
    A promise callback is still a callback, and a promise doesn't make an asynchronous operation synchronous. `checkUserValidity` cannot *return* the result. – T.J. Crowder Jun 22 '16 at 16:54
  • I don't even see any promises in your code? If there were, you could return a promise for the boolean. – Bergi Jun 22 '16 at 16:54
  • The client.query runs asynchronously, am using a postgresql npm library – Mark Omoniyi Adesina Jun 22 '16 at 16:57
  • @MarkOmoniyiAdesina: Yes, it's asynchronous, but as Bergi said, there are no promises involved. You could use a lib to promise-ify it (there are a few that bridge NodeJS APIs to promises). – T.J. Crowder Jun 22 '16 at 16:59

0 Answers0