Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda