0

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. 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.

7
  • Which version you are using? If you are using node 5, take a look at this package github.com/kriasoft/node-sqlite Commented Feb 23, 2016 at 18:18
  • It says my version is v0.10.37. Is this very out of date/worth an update? Commented Feb 23, 2016 at 18:22
  • If you want to use promises there are a lot of packages you can use. I like Q. You can also take a callback function as a parameter and call that when you're done, passing the data to the function. Commented Feb 23, 2016 at 18:24
  • Well if are just learning you might just change to latest node version, so you can have some of es6 cool stuff Commented Feb 23, 2016 at 18:25
  • I honestly can't make heads or tails of how that package is used. Commented Feb 23, 2016 at 20:39

1 Answer 1

1

If you want to use promises there are a lot of packages you can use. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.