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

103
Vistas
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 Respuestas
Responde la pregunta

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 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