Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
Retrun data from the function when it is executed

async function bringData() {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(con.query('SELECT company, industry FROM my_table', function (err, result) {
                if (err) {
                    console.error(err);
                    throw 'Error getting data from google_businesses table';
                } else {
                    return result;
                }
            }));
        }, 1000);
    });
}

(async () => {
    // return data from brindData function
    let getData = await bringData();
    console.log(getData);
})();

I am calling this function in main but I am always getting error. I am kind of new to node so if please anybody can guide me or is there any other way to do this such that when I have got my result I can return it.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Call resolve with the result from your query.The con.query looks like another asynchronous thing:

con.query('...', (err, result) => {
  if (!err)
    resolve(result);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try this.

async function bringData() {
  return new Promise((resolve, reject) => {
    con.query('SELECT company, industry FROM my_table', (err, result) => {
      if (err) {
        reject(err);
      }
      resolve(result);
    })
  });
}


(async () => {
    // execute bringData function
    try {
      let getData = await bringData();
      console.log(getData);
    } catch (e) {
      console.error(e);
    }
     
    
})();

Edited with timeout

async function bringData() {
  return new Promise((resolve, reject) => {
    con.query('SELECT company, industry FROM my_table', (err, result) => {
      if (err) {
        reject(err);
      }
      resolve(result);
    })
  });
}

function timeout(seconds = 10) {
  return new Promise(resolve => {
    setTimeout(resolve, 10 * 1000)
  })
}


(async () => {
    // execute bringData function
    try {
      await timeout();
      let getData = await bringData();
      console.log(getData);
    } catch (e) {
      console.error(e);
    }
})();

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda