node.js – NodeJS async function calls and SQLite3

One of the thing keeping me from using things like NodeJS is ascynchonious function call, I cannot seem to get a grasp on it. At the moment I’m forcing myself to rewrite a Perl/Dancer2 app to NodeJS. Seems other maintainers cannot make head or tails of Perl. Go figure :S

Anyway, I do run into a async issue once again. And made myself a test cast. It looked like I finally got the beast going. But for reasons I do not understand its SQLite which is action up now.

I made myself the following little script:

const sqlite3 = require('sqlite3').verbose();

const db = new sqlite3.Database('./db/presence.sqlite');

function getLocaties(){
    return new Promise((resolve)=>{
        var locaties = [];
        var qry = "Select naam From locaties";
        db.all(qry,[],(err,row)=>{
            if(err){throw err;}
            locaties.push(row);
        });
        resolve(locaties);
    });
}

async function asyncCall(){
    const result = await getLocaties();
    console.log(result);
    console.log('blaat');
}
console.log('asyncCall returns: ');
asyncCall()
.then(() =>{
    console.log('doing it flat: ');
    var qry = "Select naam From locaties";
    db.all(qry,[],(err,row)=>{
        if(err){throw err;}
        console.log(row);
    });
});

Which renders the following output:

asyncCall returns: 
[]
blaat
doing it flat: 
[
  { naam: 'presence.ict.writer' },
  { naam: 'presence.test.reader' },
  { naam: 'presence.test.writer' },
  { naam: 'presence.test2.writer' }
]

I fail to understand why the JQLite query returns an empty result in getLocaties()

Read more here: Source link