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

179
Visualizações
How to run a request in a function in Nodejs?

I have the following code to run a request and return the result in NodeJS :

const https = require('https')

async function GetStatus(HostName, Path) {  
    const options = {hostname: HostName,port: 443,path: Path,method: 'GET'}                     
    const req = https.request(options, res => {     
        res.on('data', d => {
            var result = JSON.parse(d);
            console.log('A - the status is ' + result.status);
            return result.status;               
        })
    })
    
    req.on('error', error => {console.error(error)});
    req.end();  
}

async function T() {
    var x = await GetStatus('www.domain.com', '/path');     
    console.log('B - The status is ' + x);
}

T();

I'm trying to run the function GetStatus to get some results based on the https request.

When running this code, I'm getting A - The status is something while the output of the function is B - The status is undefined

Does anyone know how to solve that please ? such that the function returns the result as expected instead of undefined.

Thanks. Regards,

about 4 years ago · Juan Pablo Isaza
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