Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Function return not being printed in console

I have this piece of code I'm writing in node.js, and I have a problem.

async function CheckearWhitelist(id) {
  const oracheck = ora("Checkeando en la whitelist...").start();
  con.connect(function (err) {
    if (err) {
      oracheck.fail(
        "Ha habido un fallo al conectarse a MySQL - Comprueba la conexión"
      );
    }
    con.query(
      `SELECT * FROM \`whatsapp\`.\`whitelist\` WHERE \`contacto\` = '${id}'`,
      function (err, result, fields) {
        if (err) {
          oracheck.fail("Fallo de MySQL");
        } else if (result == []) {
          oracheck.warn("Usuario no encontrado en la whitelist");
          return false
        } else {
          date = new Date();
          horas_fecha = date.getHours();
          minutos = date.getMinutes();
          hora = `${horas_fecha}.${minutos}`;

          hora_desb = result[0].hora_desbloqueo;
          if (hora_desb == 0) {
            oracheck.info("El contacto está en la whitelist indefinidamente");
            return true
          } else if (hora_desb <= hora) {
            oracheck.warn("Usuario no encontrado en la whitelist");
            return false
          } else {
            oracheck.succeed("Resultado Encontrado: ");
            return true
          }
        }
      }
    );
  });
}

CheckearWhitelist("34676654601@c.us").then((value) => console.log(value));

The thing is that if I make console.log(true/false) and not the return, it perfectly works. But when I return true/false, and make a console.log when calling the function, it doesn't work. I hope someone can help me. Thanks

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The issue is that any return value from the asynchronous function you have there won't be returned back as you expect (it will be discarded or be used by the lib - but not your code)

You should instead return new Promise object at top level and resolve it inside the callback when you want to "return" values.

function CheckearWhitelist(id) {
  return new Promise((resolve, reject) => {
    const oracheck = ora("Checkeando en la whitelist...").start();
    con.connect(function(err) {
      if (err) {
        oracheck.fail(
          "Ha habido un fallo al conectarse a MySQL - Comprueba la conexión"
        );
      }
      con.query(
        `SELECT * FROM \`whatsapp\`.\`whitelist\` WHERE \`contacto\` = '${id}'`,
        function(err, result, fields) {
          if (err) {
            oracheck.fail("Fallo de MySQL");
          } else if (result == []) {
            oracheck.warn("Usuario no encontrado en la whitelist");
            resolve(false)
          } else {
            date = new Date();
            horas_fecha = date.getHours();
            minutos = date.getMinutes();
            hora = `${horas_fecha}.${minutos}`;

            hora_desb = result[0].hora_desbloqueo;
            if (hora_desb == 0) {
              oracheck.info("El contacto está en la whitelist indefinidamente");
              resolve(true)
            } else if (hora_desb <= hora) {
              oracheck.warn("Usuario no encontrado en la whitelist");
              resolve(false)
            } else {
              oracheck.succeed("Resultado Encontrado: ");
              resolve(true)
            }
          }
        }
      );
    });
  })
}

CheckearWhitelist("34676654601@c.us").then((value) => console.log(value));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!