javascriptweb-sql

Scope of variable in WebSQL


I have this function

const _getCountriesByContinentCode = continent_code => {
    const someDb = SQLite.openDatabase(db);
    let foo = [];
    someDb.transaction(tx => {
        tx.executeSql("SOME SQL", [], (tx, results) => {
            for (let i = 0; i < results.rows.length; i++) {
                foo.push((results.rows.item(i)));
            }
            console.log('####', foo) // this logs the result as expected
        });
    });
    console.log('****', foo) // this does not log the pushed result, but an empty array 
}

The console.log('####', foo) logs as expected the results of foo.push(...). However, the console.log('****', foo) logs an empty array.

Why is that? Is someDb.transaction(... asynchronus?


Solution

  • Indeed, as @Bergi suggest in the comment below the Q: SQL.transaction and SQL.executeSql are both asynchronous.