Node.JS - 在执行代码之前等待函数完成
我不是一个整体具有Node.JS和Javascript的专业人员,所以请原谅我,如果这是一个愚蠢的问题。Node.JS - 在执行代码之前等待函数完成
我正在运行一个Node.JS服务器来接受我将要运行的网站的套接字连接,并且这个脚本要做的一部分是联系数据库。我有一个功能,旨在在启动服务器之前执行此操作。但是,当我调用该函数时,它会继续执行代码的其余部分,并且不会等待该函数完成。我尝试过使用回调但无济于事,我不知道如何使用Promise。有人可以解释我如何解决这个问题?
这里是我的参考代码:
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功能完成之前立即执行。我如何让它们一个接一个执行?
NodeJS
和JavaScript
利用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?
男人,我现在感觉很傻。 x.x的确很简单。我正在尝试这种解决方案,但认为这样做不会奏效,所以我没有。好吧。不管怎样,谢谢你。
不用担心,这只是一次错误。我会推荐给已发布的重复问题添加书签,它写得非常好,并解释了JavaScript中同步/异步行为的工作原理。如果您还不完全了解它,请不要担心,只需在需要时随时查阅以供参考。 – shotor
简单地把它回调或返回承诺... –