简体   繁体   English

Node.js SQLite 3返回承诺

[英]Node.js SQLite 3 Return Promise

I am somewhat new to node.js and I figured out how to use it with SQLite but I would like to make wrapper functions so I don't have to have SQL statements all over my code. 我对node.js有点陌生,我想出了如何在SQLite中使用它,但是我想创建包装函数,因此不必在我的代码中都包含SQL语句。 This code prints out the result of the query: 此代码输出查询结果:

 Users.find_by_id = function(id){
  db.all("SELECT * from users WHERE id=" + id, function(err, row){
    console.log(row);
  });
};

I know that node.js is asynchronous so I can't return the values themselves but I was wondering if it was possible to return a promise with the values in question. 我知道node.js是异步的,所以我自己不能返回值,但是我想知道是否可以使用有问题的值返回承诺。

If you want to use promises there are a lot of packages you can use. 如果您想使用promise,则可以使用很多软件包。 I like Q. Without promises you can take a callback function as a parameter and call that when you're done, passing the data to the function. 我喜欢Q。没有承诺,您可以将回调函数作为参数,并在完成后调用该函数,将数据传递给该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM