Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

javascript - 如何同步Node.js函数(How to synchronize Nodejs functions)

I am working on a NodeJs server and I am implementing a Sqlite3 database.(我正在使用NodeJs服务器,正在实现Sqlite3数据库。)

My problem is that the "saveMddToDb" method starts before the "createMddTables" method is finished.(我的问题是“ saveMddToDb”方法在“ createMddTables”方法完成之前开始。) So I would like to know how to wait until the end of the first one before starting the second one.(所以我想知道如何等到第一个开始之后再开始第二个。) const db = require('./db.js'); const myDb = new db(configServer.dataBase.useDatabase === true ? configServer.dataBase.name : null); myDb.createMddTables(); myDb.saveMddToDb(dataModel); class Db { constructor(name) { this.db = new sqlite3.Database(name === null ? ':memory:' : name, err => { if (err) { return console.error(err.message); } console.log(colors.yellow(`Connected to the ${name === null ? 'memory' : name} SQlite database`)); }); } close() { this.db.close(err => { if (err) { return console.error(err.message); } console.log(colors.yellow('Close the database connection')); }); }; createMddTables() { this.createTableContacts(); }; createTableContacts() { const sql = `CREATE TABLE IF NOT EXISTS contacts ( id TEXT PRIMARY KEY, firstName TEXT NOT NULL, lastName TEXT NOT NULL, nickName TEXT NOT NULL, profilePicture BLOB)`; this.db.run(sql, err => { if (err) { return console.log(err.message); } console.log(colors.yellow('Table contacts created')); }); }; saveMddToDb(mddJson) { if(mddJson) { const sqlContacts = `INSERT OR IGNORE INTO contacts VALUES (?, ?, ?, ?, ?)`; mddJson.contacts.forEach(contact => { this.db.run(sqlContacts, [uuidv4(), contact.firstName, contact.lastName, contact.lastName, contact.profilePicture], err => { if (err) { return console.log(err.message); } console.log(colors.yellow('Contacts added to database')); }); }); } } }; module.exports = Db;   ask by GuillaumeF translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this by using Promises .(您可以使用Promises做到这一点。)

Also, as suggested before, you can use async/await , which is syntactic sugar for Promises, so you can write your code like synchronous code.(另外,如前所述,您可以使用async / await ,它是Promises的语法糖,因此您可以像同步代码一样编写代码。) An async/await function always returns a Promise, so before using it I'd recommend you look up Promises first, and try to write your code with them.(异步/等待功能始终返回Promise,因此在使用它之前,我建议您先查找Promises,然后尝试用它们编写代码。) As soon as that works, you can rewrite it using async/await, so you know you fully understand what you're using and why it works!(一旦可行,您就可以使用async / await重写它,因此您知道自己完全了解自己在使用什么以及它为什么起作用!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...