Node.JS - 在执行其余代码

时间:2017-06-29 13:41:00

标签: javascript node.js asynchronous

我不是Node.JS和Javascript的专业人士,所以如果这是一个愚蠢的问题,请原谅我。

我正在运行一个Node.JS服务器来接受我将要运行的网站的套接字连接,而这个脚本的部分目的是联系数据库。我有一个功能,旨在启动服务器之前执行此操作。但是,当我调用该函数时,它仍然继续使用其余的代码,并且不等待函数完成。我尝试过使用回调但无济于事,我也不知道如何使用Promises。有人可以解释我是如何解决这个问题的吗?

以下是我的参考代码:

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);

但是这似乎在GetCompetitionDetails函数完成之前立即执行。如何让它们一个接一个地执行?

1 个答案:

答案 0 :(得分:1)

NodeJSJavaScript使用callbacks来控制应用程序的流程。 Promises简化了代码,因此您最终无法使用callback hell

最简单的修复方法是将您的作业转移到回调中:

// 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);

});

有关所有这些的详细信息,请在重复的问题中查看答案:How do I return the response from an asynchronous call?