A
A
Alexey Vladimirovich2015-09-18 14:11:33
Node.js
Alexey Vladimirovich, 2015-09-18 14:11:33

How to achieve synchronous execution?

Before performing any action, I need to check the client's balance using the getBalance() function.
The bottom line is that getBalance inside executes a mysql query and returns the balance, and as you probably already guessed, getBalance does not have time to complete the execution, as the following block code continues its work with an empty variable that must be filled with the client's balance.
What to do in this case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tex0, 2015-09-18
@Genocide

Forward the callback to getBalance, where you substitute it in place of the mysql query completion callback

/*Объявляем*/
getBalance(resultCallback) {
    connection.query('%QUERY%', resultCallback);
}

/*Используем*/
getBalance(function(err, rows, fields){
// что-то делаем с полученным результатом
});

like so

I
Ivan, 2015-09-18
@LiguidCool

Use callbacks.
In short, you pass a function to the getBalance function that should be executed when this very balance is received.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question