如何使用async和promise创建函数返回值?

时间:2015-05-12 01:38:51

标签: javascript node.js promise pg

我正在学习pg-promise。我的问题很简单。我想创建一个函数,如果它找到了结果,则返回true。但由于异步,它一直在返回假。有人可以给我一个全面的建议如何处理野兽(异步)??

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;
    }

0 个答案:

没有答案