A
A
Al2016-11-08 17:23:15
JavaScript
Al, 2016-11-08 17:23:15

How to execute sql query using async/await in node 7?

Let's say I'm using node-mysql from npm to run sql queries. With the advent of node 7 and its support for async / await, I would like to understand how you can apply this to execute sql queries? The task as a whole is to write a wrapper class for node-mysql, so that later you can do something like:
let data = new MySqlWrapper().select(query);
well, as a result, in data we have the result of the query.
Tell me how you can implement this in this example (it does not work, we have undefined in res):

(async () => {
            let getData = async ()=> {
                db.query(`select * from users`,null,(err,data) =>{
                    console.log('ok');
                    return data;
                });
            };

            var res = await getData();
            //нужно чтобы в res пришли данные запроса
})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zekohina, 2016-11-08
@zekohina

If select returns a promise, then like this:

async function run() {
 let data = await (new MySqlWrapper()).select(query);
}

run();

In this case, the node must be launched with the --harmony flag

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question