I want to iterate through my sqlite database synchronously.
Using async and sleep in db.each() does not delay the each callback since it does not use await on the callback internally:
var sleep = require("sleep");
var sqlite3 = require("sqlite3");
sqlite3.verbose();
var sqlite = require("sqlite");
(async () =>
{
var db = await sqlite.open({
filename: "./mydatabase.db",
driver: sqlite3.Database
});
// doesn't await this async callback before calling the next
// row result function
await db.each(`SELECT * FROM mytable`, async (err, row) =>
{
await sleep(10000);
console.log(row);
});
})();
I don't want to use .all() since it will take a long time to load hundreds of thousands of records into memory.
I also don't want to use LIMIT x OFFSET y since it will require re-running the query multiple times for each section that I would check making it slower.
How can I iterate the sqlite results synchronously so that the next row isn't fetched until I finish processing the current row?
In PHP it would be similar to this kind of loop:
// fetches one result at a time and does not continue until
// I call the next `mysql_fetch_row`
while(($row = mysql_fetch_row($queryResult))
{
var_dump($row);
sleep(123);
}
I want to get one result at a time rather than be flooded with hundreds of thousands of callback functions being called without pause.