-1

I have this server in Node.js using socket.io. I have a function

function updatePoints(){
    pool.getConnection(function(err, connection) {
        connection.query("SELECT * FROM `users`", function(error, rows) {
           //here it fetchs the users' points and emit to their respective sockets
        });
        connection.release();
    });
}

Alright, when I call this function inside another one, it seems to run before the codes that come before it. Here:

function(){
    connection.query('UPDATE `users` SET '...});

    connection.query('UPDATE `users` SET '...});

    updatePoints();
}

This second function is when a user is donating points to another one, so I decrease the points from the donor and increase the receiver's points. Funny thing that when the function is called it emits for the donor just like I want it to do. The donor sees his points updating real time, while in the receiver's side he can't see it, unless the other one donates him points for a second time, then he sees the first donation going on. If the function is running before if should, how can I make it work?

Andy
  • 49,085
  • 60
  • 166
  • 233
Marciel Fonseca
  • 371
  • 1
  • 4
  • 19
  • It's very hard to understand what question you want help with, but it seems like maybe you don't understand how an asynchronous operation works in Javascript. They finish sometime later, but other code in sequence continues to run. If you want to sequence async things, you can't do it like you're doing in your last code block. – jfriend00 Jan 09 '16 at 05:30
  • @jfriend00 So there's no easy way to achieve what I'm trying to? I mean, update 2 records on the database then run the function that fetch all that data? – Marciel Fonseca Jan 09 '16 at 08:04
  • It's not hard with proper asynchronous programming. If you described in your question exactly what you want to accomplish, I could write an answer that showed how to do that. Your question does not clearly state what your goal is - all it describes is some problems you are having so I'm not sure exactly what to recommend for your problem. And, trying to teach the whole topic of asynchronous programming without focusing on a specific problem of yours is far too big a subject for an answer. – jfriend00 Jan 09 '16 at 08:07
  • Ok, as You can see I make two updates to change the user's points in the database, then I call the function updatePoints() which fetches the points on the database and send to client, but seems that the function fetches the points before they're changed. I want it to actually query the database after the UPDATE query has been made/finished. Sorry if I'm a bad explainer but my English is too poor. Thank You sir @jfriend00 – Marciel Fonseca Jan 09 '16 at 08:22

2 Answers2

1

connection.query() is asynchronous. That means that calling it just starts the operation and it then runs in the background. Meanwhile, the next line of Javascript runs right away while the connection.query() is going on in the background. So, if you do:

connection.query(...);
someFunction();

Then, as you have discovered, someFunction() will execute before connection.query() finishes.

The solution is to sequence your work using the completion callback of the connection.query() operation by putting anything that should happen after the query completes inside the callback itself.

 connection.query(..., function(err, rows) {
     // execute some function after the query has completed
     someFunction();
 });

This can be extended to more levels like this:

 connection.query(..., function(err, rows) {
     // execute another query after the first one
     connection.query(..., function(err, moreRows) {
         // execute some function after both queries have completed
         someFunction();
     });
 });
 // code placed right here will execute BEFORE the queries finish

Now standard in ES6 and available in many libraries, the concept of promises can also be used to coordinate or synchronize asynchronous operations. You can read more about promises in zillions of places on the web. One introductory explanation is here on MDN.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I kinda knew that, but i thought the update query could complete quickly enough... But I didn't think in your solution and Im sure it will work, thank You! – Marciel Fonseca Jan 09 '16 at 12:01
0

I don't really understand what you are trying to say but it seems like a closures problem. If that is the case may be this How do JavaScript closures work? can help you out.

It explains closures in detail.

Community
  • 1
  • 1
  • Isn't that supposed to work? I mean, i make the database update then call the function, shouldn't it be called after the updates? – Marciel Fonseca Jan 09 '16 at 08:06