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

105
Visualizações
Async function returning promise but console.log does give out the result as wanted | NodeJS Own Module

I just wrote a simple and small API to define the gender of a person. It works via a get-request to a page which gives me back a JSON object.

This is the code of my module:

'use strict';
const https = require('https');

exports.getGender = function (name) {
  return new Promise((resolve) => {
    //send request to api
    https.get(`https://api.genderize.io?name=${name}`, (resp) => {
      var data = '';
      //replace data
      resp.on('data', (chunk) => {
        data += chunk;
      });
      //define information on end of query
      resp.on('end', () => {
        var info = JSON.parse(data);
        //check accuracy of gender
        if (info.probability >= 0.8) {
          //check gender
          if (info.gender == 'male') {
            resolve('Sehr geehrter Herr');
          } else if (info.gender == 'female') {
            resolve('Sehr geehrte Frau');
          }
        } else {
          resolve('Sehr geehrte/r Frau / Herr');
        }
      });
    });
  });
};

And this is the call I do in the main file:

//get gender of person
async function getG(name) {
  const data = await genderApi.getGender(name);
  return data;
}

getG('Tim');

But the output is just Promise { <pending> }, If I replace the return data with a console.log(data) it just works fine and it shows me the output in the console but I really need it to return the value cause later on I use this function in a text back to the user

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

getG function is also async so you must use with await keyword or .then callback

const val = await getG('Tim');
// or
getG('Tim').then((val) => {
 console.log(val)
})
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