1
Greetings
I’m developing a synchronizer between two databases (SQL Server and MySQL) with the ElectronJS and the Node.JS and everything is working fine, but I would like it to perform synchronization of all tables (which are done asynchronously within a loop for) he executed the method window.close() to close the application.
// Removi os tratamentos de erro e conexão com o banco, porque estão funcionando
// Leio um arquivo .json que possui todas as tabelas com as colunas que eu desejo
fs.readFile(ABSPATH + 'tables.json', 'utf8', async (err,json) => {
  // Converte o conteúdo do arquivo pra um objeto JSON
  let vetor = JSON.parse(json)
  // Para cada tabela
  for (let i = 0; i < vetor.length; i++) {
    // Lê a tabela no SQL Server e salva os dados no MySQL
    await read(vetor[i].table,vetor[i].keys)
  }
  // Ao invés de fechar, estou só exibindo na tela essa mensagem (pra debug)
  document.body.innerHTML += "<h2>Todos os dados foram inseridos</h2>"
})But, as you can see, it is returning the final result before returning the functions, that is, they remain asynchronous:
 
I believe my mistake is at the time of saving the data, in the following function, because I tested it with the
console.log(), but still, I can’t make it synchronous:con.connect(async err => {
  // Começo a Transaction
  await con.beginTransaction(async err => {
    // Limpo a tabela
    await con.query(`TRUNCATE TABLE ${table}`, err => {})
    // Loop pra inserir todos os dados na tabela
    for (let i = 0; i < values.length; i++){
      // Crio e executo um insert
      await con.query(createInsert(table,keys,values[0]), err => {})
    }
    // Encerro a Transaction quando está tudo inserido
    await con.commit(err => {
        // Escrevo na tela que os dados foram salvos
        document.body.innerHTML += `<p>Dados de ${table} cadastrados com sucesso.</p>`
    })
  })
  // Fim da Transaction
})
Thank you, it worked as expected (at least this problem).
– Nary Luiz