[英]Nodejs Asynchronous reusable function call
I have some problem developping my own Node.JS application. 我在开发自己的Node.JS应用程序时遇到了一些问题。 I'm new to this kind of code architecture and not so good with asynchronous as well.
我是这种代码架构的新手,对异步也不太好。
I currently have an event in my index.js script, calling for another script database.js . 我的index.js脚本中当前有一个事件,需要另一个脚本database.js 。 This database script will handle every db call inside my application.
该数据库脚本将处理我的应用程序中的每个数据库调用。
index.js index.js
const db = require('database.js');
//Called when a userjoin the application, this is working well//
db.userJoinedFunction("username");
I was planning adding other function call inside my "userjoinedFunction". 我打算在“ userjoinedFunction”中添加其他函数调用。 For example : establishing first the DB connection --> 2nd checking if the user is already inside the DB --> 3d if not, create the user entry --> 4 if yes, do welcome back message.
例如:首先建立数据库连接->第二个检查用户是否已经在数据库中->如果不是3d,则创建用户条目-> 4,如果是,请返回欢迎消息。 Since I have to establish the DB connection multiple time, I wanted to reuse some code inside theses function like this :
由于必须多次建立数据库连接,因此我想在这些函数中重用一些代码,如下所示:
database.js database.js
module.exports.userJoinedFuntion = userJoinedFunction
function userjoinedFunction(username){
Init_database_connection();
if(UserAlreadySignedup(username)){
WelcomeMessage(username);
}
else {
AddUserToDatabase(username);
}
}
but obviously i'm doing it wrong because asynchronous Node.js application can't be that simple... How can I have that type of reusable architecture ? 但是显然我做错了,因为异步Node.js应用程序不能那么简单...我如何拥有这种可重用的体系结构? Do you have articles I can read about the subject ?
您是否可以阅读有关该主题的文章?
Thank you very much for your help ! 非常感谢您的帮助 !
EDIT 29/05/2019 编辑29/05/2019
with the response of novomanish I tried this : 随着novomanish的回应,我尝试了以下方法:
async fonction run(){
await(call("number 1");
await Init_database_connection();
await(call("number 2");
}
async function call(text){
console.log(text);
}
async function Init_database_connection(){
db = new sqlite3.Database("path", sqlite.OPEN_READWRITE, (err) =>{
if(err){ console.log(err); }
else { console.log("DB Connected"); }
});
}
my output are : 我的输出是:
- number one
- number two
- DB Connected
I checked many article and documentation, I can't find a good way of reusing function inside a "main function" (like in my example : the function run()). 我检查了许多文章和文档,找不到在“主要功能”内重用功能的好方法(例如在我的示例中:功能run())。 And i'm surprised that waiting for finished DB query isn't apparently thing...
而且令我惊讶的是,等待完成的数据库查询显然不是一件事情……
You need async/await in your functions. 您需要在功能中进行异步/等待。 So the new code would look like:
因此,新代码如下所示:
async function Init_database_connection(){...}
...
async function userjoinedFunction(username){
await Init_database_connection();
if(await UserAlreadySignedup(username)){
return WelcomeMessage(username);
}
return AddUserToDatabase(username);
}
Regarding your edit, Init_database_connection should have returned a promise for await to work, like this: 关于您的编辑,Init_database_connection应该返回一个等待工作的承诺,如下所示:
async function Init_database_connection(){
return new Promise((resolve, reject)=>{
db = new sqlite3.Database("path", sqlite.OPEN_READWRITE, (err) =>{
if(err){
console.log(err);
reject();
}else {
console.log("DB Connected");
resolve()
}
});
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.