Categories
Database Development MySQL SQL

How to handle async call to mysql database

I’m pretty new to nodejs which is probably why I’m asking this question. I recently discovered that calls being made with nodejs to any database are async.

As a former C# .Net programmer this is little bit a surprise for me. I’m just used to code synchronous and it’s ok to wait a little.

Currently I want to make a database call and with the returned result the code should continue to run. How to do this best? I found something about promises but I can’t find the proper solution yet.

What I really want is something like this:

var requestLoop = setInterval(function(){

  console.log('Trading bot (re)started..');
  var wlist = [];

  wlist = db_connection.getWatchList_DB() ==> Database call here 


    if(wlist.length > 0){
      // Perform the rest of the code

    }

}, 5000);//300000 five minutes

So, for me it’s ok to wait for the database call and continue with the fetched results. Is there any simple solution for this?

Leave a Reply

Your email address will not be published. Required fields are marked *