Is it possible to wait until the query is finished? I have something like:
function query(fields, table) {
db.on('open', () => {
return db.prepare(`SELECT ${fields} FROM ${table}`).all()
})
}
let rows = query('*', 'user')
console.log(rows) // is returning undefined. I need this to wait until all() is finished.
thanks!
There is no synchronous function and there shouldn't be one.
Use a callback (or promises). You need to understand the way javascript works, what async means etc.
Also check out the async/await stuff for ES7.
function query(fields, table, callback) {
db.on('open', () => {
return callback(db.prepare(`SELECT ${fields} FROM ${table}`).all());
})
}
let rows = query('*', 'user', function(rows) {
console.log(rows) // is returning undefined. I need this to wait until all() is finished.
});
@C3realGuy Sqlite's C API is synchronous – this is a very reasonable request.
You can use better-sqlite3
Most helpful comment
@C3realGuy Sqlite's C API is synchronous – this is a very reasonable request.