Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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 answers
Answer question

0

Try this:

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!