2

I am not a professional with Node.JS and Javascript as a whole, so please forgive me if this is a stupid question.

I am running a Node.JS server to accept socket connections for a website that I will be running, and part of what this script is meant to do is contact a database. I have a function that is designed to do this before starting the server. However, when I call the function it continues with the rest of the code anyway and does not wait for the function to finish. I have tried using callbacks but to no avail, and I have no clue how to use Promises. Can someone explain how I resolve this issue?

Here is my code for reference:

function GetCompetitionInfo(competitionID,callback){
    var competitionInfo = {
        id:competitionID,
        title:"",
        topic:"",
        difficulty:"",
        description:"",
        jackpot:0,
        answer:0,
        author:0
    }
    var testVariable;
    sqlConnection_read.connect(function(err){
       if(err) throw err;
       console.log("Connected to TSDB, retrieving competition info...");
       var query = "SELECT * FROM competitions WHERE PuzzleID="+competitionID+";";
       sqlConnection_read.query(query,function(err,result){
          if(err) {console.log("Uh oh spaghettiohs");throw err;}
          competitionInfo.title = result[0].Puzzle_Title;
          competitionInfo.topic = result[0].Puzzle_Topic;
          competitionInfo.difficulty = result[0].Puzzle_Difficulty;
          competitionInfo.description=result[0].puzzle_description;
          competitionInfo.answer=result[0].Puzzle_Answer;
          competitionInfo.jackpot=result[0].Puzzle_Jackpot;
          competitionInfo.author=result[0].Puzzle_Author;  
          callback(competitionInfo);
       });
    });
}
GetCompetitionInfo(0,function(data){
   competitionInfo=data;//This is a variable outside of the scope
});
//I want this to be executed afterwards
var competitionTitle = competitionInfo.title;
var competitionTopic = competitionInfo.topic;
var competitionDifficulty = competitionInfo.difficulty;
var competitionDescription = competitionInfo.description;
var competitionAnswer = competitionInfo.answer;
var jackPotTotal = competitionInfo.jackpot;
var competitionAuthor = competitionInfo.author;

console.log("Title:"+competitionTitle);
console.log("Topic:"+competitionTopic);
console.log("Difficulty:"+competitionDifficulty);
console.log("Description:"+competitionDescription);
console.log("Answer:"+competitionAnswer);
console.log("Jackpot:"+jackPotTotal);
console.log("Author UID:"+competitionAuthor);

But this seems to immediately execute before the GetCompetitionDetails function has finished. How do I make them execute one after the other?

1 Answers1

1

NodeJS and JavaScript makes use of callbacks to control the flow of your application. Promises simplify the code so you don't end up in something called callback hell.

Easiest fix it to move your assignments into the callback:

// function(data) { ... } is the callback that will be executed AFTER GetCompetitionInfo is finished. Anything you want to do after will have to be in that function.
GetCompetitionInfo(0, function(data){

   // everything in this block will be run AFTER GetCompetitionInfo is finished
   competitionInfo=data;

    var competitionTitle = competitionInfo.title;
    var competitionTopic = competitionInfo.topic;
    var competitionDifficulty = competitionInfo.difficulty;
    var competitionDescription = competitionInfo.description;
    var competitionAnswer = competitionInfo.answer;
    var jackPotTotal = competitionInfo.jackpot;
    var competitionAuthor = competitionInfo.author;

    console.log("Title:"+competitionTitle);
    console.log("Topic:"+competitionTopic);
    console.log("Difficulty:"+competitionDifficulty);
    console.log("Description:"+competitionDescription);
    console.log("Answer:"+competitionAnswer);
    console.log("Jackpot:"+jackPotTotal);
    console.log("Author UID:"+competitionAuthor);

});

For more info about all of this check the answer in the duplicate question: How do I return the response from an asynchronous call?

shotor
  • 763
  • 6
  • 17
  • 1
    Man, I feel rather silly right now. x.x It really was that simple. I was going to try that solution but thought that would have not worked so I didn't. Oh well. Thank you anyway. <3 – British Chap Named Charles Jun 29 '17 at 14:46
  • No worries, it's a mistake you only make once. I would recommend bookmarking the duplicate question that was posted, it's very well written and it explains how synchronous/asynchronous behavior works in JavaScript. Don't worry if you don't understand all of it yet, just keep it around for reference when you need it. – shotor Jun 29 '17 at 14:51