I'm working on a node project with sqlite as my db.
function select_from_table(tablename, limit) {
let arr = [];
let sql = `SELECT id FROM ${tablename} WHERE Type='Real' LIMIT ${limit}`;
db.each(sql, function (err, row) {
console.log(row.PropertyId);
arr.push(row.PropertyId);
});
return arr;
}
when I ran:
console.log(select_from_table('myData', 4));
I get '[]'
stepping through this I see that control goes right to return array and then goes back to the loop above to perform the select, so that the arr is never returned. I then tried:
async function select_from_table(tablename, limit) {
let arr = [];
let sql = `SELECT id FROM ${tablename} WHERE Type='Real' LIMIT ${limit}`;
await db.each(sql, function (err, row) {
console.log(row.PropertyId);
arr.push(row.PropertyId);
});
return await arr;
}
but this returns a promise. What's the best way to approach returning the selected result?