Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
Incorrect value after calling promise async function in js

I'm trying to make a function that checks how many records in database and sends the number to variable:

var records = -1;

async function howManyRecords() {
    let myPromise = new Promise(function (resolve) {

        DB.transaction(function (tx) {
            tx.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function (tx, rs) {
                records = rs.rows.item(0).mycount;
                resolve();
            },);
        });
    });
    await myPromise;
    console.log("records from promise function: " + records);
    return myPromise;
}

Then I would like to use the number in another functions, for example display in the console log when device is ready:

onDeviceReady.then(function () {
    howManyRecords();
    console.log("after calling function records are: " + records);
}

The howManyRecords function counts records correctly, but then variable records is not correct:

IMG: after calling function records are: -1 // records from promise function: 5

I really need some help, I'm new to the topic and stuck here since a long time and can't go on. I'm using cordova sqlite.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this:

onDeviceReady.then(async function () {
await howManyRecords();
console.log("after calling function records are: " + records);}
about 4 years ago · Juan Pablo Isaza Relatório

0

This problem is not for your asynchronous behavior. Here actually you re assign the value of records variable in the function. That's why the value of records change. If you want to get rid of from the issue you can take another variable in the function for counting records. And initialize the variable with let. As let is block scope, that's why its' value will not go through the function.

var records = -1;

async function howManyRecords() {
    let records = -1;

    let myPromise = new Promise(function (resolve) {

        DB.transaction(function (tx) {
            tx.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function (tx, rs) {
                records = rs.rows.item(0).mycount;
                resolve();
            },);
        });
    });
    await myPromise;
    console.log("records from promise function: " + records);
    return myPromise;
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda