Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

0

Try this:

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

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda