V
V
vetsmen2017-01-30 01:10:09
JavaScript
vetsmen, 2017-01-30 01:10:09

Asynchrony, promises, exports, database query. How?

Good day. There is a model for receiving data, of the form:

var data = {};
data.one = function(){};
data.two = ...
module.exports = data;

In data.one I want to query the db and get the response:
connection.query('SELECT balance FROM Users WHERE id = ?', id, function(error, result, fields){
    	if(error)
    		throw(error);
         return result;
    });

So, due to asynchrony, this thing does not have time to be completed for me and the module does not receive the necessary data.
How can I deal with this? I looked at promises, but I have little idea how to apply them in this case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2017-01-30
@vitali1995

Learn async/await - should help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question