3

I'm querying my database in an async function (via the mysql library), but I can't seem to get my function to properly wait for the query to finish before continuing.

Code:

async (eventName, eventArgs, app) => {

  ...

  prefix = "!";

  if(msg.channel.type !== "dm")
    database.query(`SELECT prefix FROM guilds WHERE \`id\`=${msg.guild.id}`, (err, res) => {
      if(err)
        return console.error("db", `${err.message}`);

      if(res.length > 0)
        if(res[0].prefix)
          prefix = res[0].prefix;

      console.log(`from within callback ${prefix}`);
      console.log(`returned from query ${res[0].prefix}`);
    });

  console.log(`prefix in standard flow ${prefix}`);

  ...

}

The console outputs the following:

prefix in standard flow !
from within callback -
returned from query -

All 3 should output -, but outside the query callback it is !. It appears to be that my async function isn't waiting for the query callback function to finish before continuing, not a scope issue.

I've also tried awaiting the database.query function, but to no avail.

7
  • You need to await a promise. Commented Aug 4, 2017 at 17:48
  • @SLaks I'm aware of that, however not sure how to go about it — I'm completely clueless when it comes to creating promises. When you say that, do you mean that I need to create it surrounding the database.query function, then resolve the promise once the value of prefix has been set? Commented Aug 4, 2017 at 17:55
  • Yes, exactly. You should wrap your async functions to return promises. Commented Aug 4, 2017 at 17:58
  • Consider using mysql2, which has built-in promise support. Commented Aug 4, 2017 at 18:00
  • @SLaks This answer showed me how — thank you for letting me know that's the correct way to do it, I'll definitely be doing a lot of that in the future. Commented Aug 4, 2017 at 18:59

1 Answer 1

2

Using async/await:

async (eventName, eventArgs, app) => {
  let prefix = '!';
  const query = new Promise((resolve, reject) => {
    database.query(`SELECT prefix FROM guilds WHERE \`id\`=${msg.guild.id}`, (err, res) => {
      if(err)
        return reject("db", `${err.message}`);

      if(res.length > 0 && res[0].prefix)
          prefix = res[0].prefix;

      console.log(`from within promise ${prefix}`);
      console.log(`returned from query ${res[0].prefix}`);
      resolve();
    });
  });

  await query;
  console.log(`prefix in standard flow ${prefix}`);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I've slightly modified your answer to fit my needs, but that exact same concept works perfectly for my needs. Thank you, @PeterMader!

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.