0

I want to make sure the database opens properly, then execute the get statement, then output it. But I am the following in the console where it's getting executed simultaneously. Thanks in advance!

Console log:

here1
here2
usergp is undefined
usergp in get is 10931

Desired output:

here1
usergp in get is 10931
here2
usergp is 10931

Code:

let selectGP = 'select gp from Money where userID = ? and guildID = ?';
    let userGP;
    let db;
    const openDB = new Promise(resolve => {
        db = new sqlite3.Database('./Toothless.db', (err) => {
            if (err) {
                console.error(err.message);
            }
        });
        resolve();
    });
    x = openDB.then(() => {
        console.log('here1');
        db.get(selectGP, [interactionUserID, interactionGuildID], (err, row) => {
            if (err) {
                console.log('in test > select GP');
            } else {
                userGP = row.gp;
                console.log('usergp in get is ' + userGP);
                return userGP;
            }
        });
    }).then((data) => {
        console.log('here2');
        console.log('usergp is '+ data);
        interaction.reply({
            content: 'you have ' + data
            , ephemeral: true
        });
        db.close();
    }).catch(() => {
        console.log('in catch');
    });

1 Answers1

1
  • you're not returning any value from the first step of the promise chain, thus breaking it, so data in the next step will always remain empty.
    does db.get return a promise? if so, you can return that (or declare the first step as an async function, and return the awaited value, i.e. return await db.get(...). as comments mentioned - async/await is not the point here - it's just another way to express the nested asynchronous flow you're after)
    in any case, you should also omit the db.get callback, and move that logic to the then callback.

  • it would be a good idea to throw any error from the database (after logging them or whatever), so it is caught by the later catch() step

if your aim is to change the order of execution, and db.get does not provide promises to work with, you can either wrap it with a promise yourself (tedious), or use a helper library for such flows, like async, that also has utilities for such wrappings (easy).

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
  • `db.get` fetches data from the database and the results is in the `row` variable. I am returning the variable `userGP` which is used in the next promise chain but the next promise chain is executed before my first return – Toothless LL Nov 03 '21 at 19:07
  • The mention of `async`/`await` is a red herring. Asking whether `db.get` returns a promise is important, but the next sentence should be "*if it returns a promise, you need to `return` that, if it doesn't, then you need to promisify (wrap it in another `new Promise`) and `return` that.*" – Bergi Nov 03 '21 at 19:07
  • @Bergi you are absolutely correct, i have already updated it during my editing iterations... it was an answer in a rush! thanx for noticing – Eliran Malka Nov 03 '21 at 19:08
  • @ToothlessLL, the flow is understood. i updated my answer to include more info. you can try and research further to implement it with the methods offered above – Eliran Malka Nov 03 '21 at 19:11