3

What I am doing right now is:

db.get(`SELECT * FROM '${message.guild.id}'`,(err, row) => {
    console.log(row.channel);
});

What I basically want to do is, something like this:

let xyz = db.get(`SELECT * FROM '${message.guild.id}'`,(err, row) => {
    return row.channel;
});
console.log(xyz);

1 Answer 1

5

Your db function uses a callback. A callback is run async to your code so JS will not wait for it and will continue to execute the code below. That means you won't be able to assign a variable like you suggested.

What you could do is create an async function you can await in your code:

async function getChannelFromID(db, id) {
    return new Promise((resolve, reject) => {
        db.get(`SELECT * FROM '${id}'`,(err, row) => {
            if (err) reject(err); // I assume this is how an error is thrown with your db callback
            resolve(row.channel);
        });
    });
}

// Now you can use the function like this
// (make sure you mark the function you call this in as async)
const xyz = await getChannelFromID(db, message.guild.id);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the help this works for me right now, But is there any way to do what i was trying to do without async?
Only by adding all your code inside the callback function... otherwise... nope ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.