Unable to store query result inside a variable I am trying to query user by email and save the result inside a variable. I tried using await and async, callback and promise but none of them actually work.
const checkEmailUsed = (email) =&gt; {
  let sql = &quot;SELECT * FROM users_signup WHERE user_email = ? LIMIT 1&quot;;
  return new Promise((resolve, reject) =&gt; {
    conn.query(sql, email, (err, res) =&gt; {
      if (err) {
        reject(err);
        return;
      }
      resolve(res[0]);
    });
    conn.end();
  });
};

var user = [];
checkEmailUsed(&quot;mikias@email.com&quot;)
  .then((res) =&gt; user.push(res))
  .catch((err) =&gt; console.log(err));

console.log(user); // Still []
The console.log is executed before the promise is fulfilled. This should work:
const checkEmailUsed = (email) =&gt; {
  let sql = &quot;SELECT * FROM users_signup WHERE user_email = ? LIMIT 1&quot;;
  return new Promise((resolve, reject) =&gt; {
    conn.query(sql, email, (err, res) =&gt; {
      if (err) {
        reject(err);
        return;
      }
      resolve(res[0]);
    });
    conn.end();
  });
};

var user = [];
checkEmailUsed(&quot;mikias@email.com&quot;)
  .then((res) =&gt; user.push(res))
  .then(() =&gt; console.log(user))
  .catch((err) =&gt; console.log(err));