Wait for consultation with the bank to give feedback

Asked

Viewed 41 times

0

I’m making an appointment with Sqlite Bank. But the function ends up giving return before the consultation is performed. I am trying to solve this using a type of control while the query does not proceed. Is there any way to resolve this issue?

My code is below:

model.executarSql = function (sSql) {
   var bExec = false; // Controle se pode prosseguir ou não

   db.transaction(function(tx){
      tx.executeSql(sSql, [], function(txs, results){
         var aRet  = []; // Array bidimensional com todos os resultados
         var aItem = []; // Array com um resultado por vez

         for(it = 0; it < results.rows.length; it++){
            while (aItem.length > 0){ aItem.pop() }; // limpar o array

            for(cp = 0; cp < results.rows.item(it).length; cp++){
               aItem.push(results.rows.item(it)[cp]);
            }

            aRet.push(aItem);
         }

         bExec = true;
      });
   });

   while (!bExec){
      // Aguardar a consulta ao banco
   };

   return aRet;
};
  • 1

    Rodrigo, Javascript works asynchronously. I suggest you take a look at callback and Promise. I had the same problem in understanding await Callback

1 answer

3


Friend, I suggest you use a javascript strategy.

Because then the data coming from the bank will come within the resolution of your database and in that context you will use the data or manipulate it.

function consultaBanco (msg, who, timeout) {
return new Promise((resolve, reject) => {
    resolve("Executa a sua consulta")
})
}
consultaBanco().then((resultaDaConsulta) =>
 "Aqui você você tem garantia que seu dado existira antes de manipular"
).catch((msg) => {
"Aqui é você terá o objeto de error caso aconteça"})

I hope it helped. Good luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.