Это асинхронная операция, что означает обещание. Лучшим способом было бы передать обратный вызов функции или вернуть операцию db в качестве обещания и цепочки. Некоторая документация по Обещаниям в javascript находится здесь.
С обратным вызовом:
class CartDB {
constructor(){
}
totalItems = 0;
checkCountOfProduct(callback){
query = "SELECT SUM(count) AS product_count FROM Predefinedcart";
db.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => {
console.log(results.rows.item(0).product_count)
this.totalItems = results.rows.item(0).product_count;
callback(this.totalItems)
}, function (tx, error) {
console.log('SELECT error: ' + error.message);
});
})
}
}
и в компе звоните:
CartDB.checkCountOfProduct(count => console.log(count));
С обещанием:
class CartDB {
constructor(){
}
totalItems = 0;
checkCountOfProduct(){
query = "SELECT SUM(count) AS product_count FROM Predefinedcart";
return new Promise((resolve, reject) => db.transaction((tx) => {
tx.executeSql(query, [], (tx, results) => {
console.log(results.rows.item(0).product_count)
this.totalItems = results.rows.item(0).product_count;
resolve(this.totalItems);
}, function (tx, error) {
reject(error);
});
}))
}
}
и в компе звоните: CartDB.checkCountOfProduct().then(count => console.log(count));