简体   繁体   English

测试所有已终止的功能

[英]Test all functions that have been terminated

I am developing a synchronizer between two databases ( SQL Server and MySQL ) with ElectronJS and Node.JS and everything is working fine, but I would like to execute the window.close() method to terminate the application when all tables are syncronized (which are done asynchronously within a for loop). 我正在用ElectronJSNode.JS开发两个数据库( SQL ServerMySQL )之间的同步器,并且一切正常,但是我想执行window.close()方法以在所有表​​同步后终止应用程序(在for循环中异步完成)。

 // I removed error and bank connection treatments because they are working // Read json file that contains all tables and its columns (keys) fs.readFile(ABSPATH + 'tables.json', 'utf8', async (err,json) => { // Parse the content of the file into a JSON object let vetor = JSON.parse(json) // Foreach table for (let i = 0; i < vetor.length; i++) { // Read the table from SQL Server and save it falues into MySQL await read(vetor[i].table,vetor[i].keys) } // Instead of closing, I'm just displaying this message on screen (for debugging) document.body.innerHTML += "<h2>All data where inserted</h2>" }) 


But as you can see, it is returning the final result before returning the functions, that is, they remain asynchronous: 但是如您所见,它在返回函数之前返回最终结果,也就是说,它们保持异步状态:

Click here to see the image 点击这里查看图片


I believe my error is at the time of saving the data in the following function because I tested it with console.log() , but I still can't make it synchronous: 我相信我的错误是在将数据保存到以下函数时,因为我使用console.log()对其进行了测试,但仍然无法使其同步:

 con.connect(async err => { // Begin MySQL Transaction await con.beginTransaction(async err => { // Clear the table await con.query(`TRUNCATE TABLE ${table}`, err => {}) // Loop to insert all inputs into table for (let i = 0; i < values.length; i++){ // Create and execute the query to input values into table await con.query(createInsert(table,keys,values[0]), err => {}) } // When all data are inputed, end Transaction await con.commit(err => { // Write in window that everything gonna allright document.body.innerHTML += "<p>All data where successfully saved into " + table + ".</p>" }) }) // End MySQL Transaction }) 

Like georg had told me, the problem was that I was trying to await async functions, but the await only works with Promises. 就像georg告诉我的那样,问题是我试图等待异步功能,但是await仅适用于Promises。
I found the answer greetings to him and to this answer that I had received on StackOverflow in portuguese . 我找到了对他的问候和葡萄牙语中我在StackOverflow上收到的答案
So, I just put my connection into a Promise, like this: 因此,我只是将连接放入Promise中,如下所示:

 // Save datas into MySQL Database function save(table, keys, values) { return new Promise((res, rej) => { // MSSQL returns a JSON object filled with informations that I don't want // so I filter only what I want: the result of the query values = values.recordset // Start a new MySQL connection let con = mysql.createConnection({ user: OUSER, password: OPASS, server: OHOST, port: OPORT, database: ODB }) con.connect(err => { if (err) { alert(err) window.close() } con.beginTransaction(err => { // This function is just for DontRepeatYourself rollBack(con, err, rej) con.query(`TRUNCATE TABLE ${table}`, err => { rollBack(con, err, rej) }) con.query(createInsert(table,keys,values[0]), err => { rollBack(con, err, rej) }) con.commit(err => { rollBack(con, err, rej) document.body.innerHTML += `<p>All data where successfully registered into ${table}.</p>` res() }) }) }) }) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM