0

I'm learning about pg-promise. My question is very simple. I want to create a function return true if it found a result. but it kept returning false because of async. Could someone give me a comprehensive advice how to handle the beast (async)??

function find(id){
    var found = false;
         db.one("select * from users where id=$1", id) // find the user from id; 
        .then(function(data){
            console.log(data);
            found = true;
        }, function(reason){
            console.log(reason); // display reason why the call failed; 
        })
       return found;
    }
Phi Nguyen
  • 3,046
  • 14
  • 26
  • I posted a [more specific promise oriented answer](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/30180679#30180679) on the duplicate, I'd love to get your feedback. – Benjamin Gruenbaum May 12 '15 at 02:23
  • Hi @BenjaminGruenbaum ! Thanks for giving me a clear useful answer. I understand more about Promise now. I applied the Wrapped Promise concept and it worked well. However when I tried to use ES6 generator, it returned me a Promise object with {resolve: undefined}. Maybe I still do not comprehend the Generator concept. I will read it more. By the way, Thanks very much ! – Phi Nguyen May 12 '15 at 04:30

0 Answers0