Tuesday, 11 February 2020

Execution order with database

Hi,Currently messing around with a Discord bot and I'm running into a dumb issue. Basically I'm connecting to my db, send a request, and if there's any result send another request. Sounds insanely easy but at this point my brain can't think straight after switching between C/Java and JS.Here's a sample (db and requests are all correct, for clarity I'm not copying those parts):let sqlc = 'SELECT COUNT(*) AS whatever ...' db.all(sqlc, [], (err, rows) => { if (err) { throw err; } if (parseInt(rows[0].whatever, 10) > 0){ result = true; } console.log("Debug 1: " + result); // So far so good, result == true, assuming there was any result }); console.log("Debug 2: " + result); // This is were things go south. result == false, so obviously it's executing this bit before the whole db part if (result){ // It's also executing this bit before the db part. and of course result is initalized as false so it won't work. } And this is obvious when I look at my console output: Debug 2 log is printed before Debug 1.I guess you understand the issue here, and I'm sure the solution is very simple but I just can't think of anything. Any help would be appreciated.

Submitted February 11, 2020 at 03:05PM by HappyLaifu

No comments:

Post a Comment