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

251
Visualizações
Why does my catch() handle the error event?

Can someone explain why my catch() doesn't work? I get

throw er; // Unhandled 'error' event
  ^

from this

const https = require('https');

const options = {
  hostname: 'github.comx',
  port: 443,
  path: '/',
  method: 'GET'
};

async function main() {
  options.agent = new https.Agent(options);
  const valid_to = await new Promise((resolve, reject) => {
    try {
      const req = https.request({
        ...options, checkServerIdentity: function (host, cert) {
          resolve(cert.valid_to);
        }
      });
      req.end();
    } catch (error) {
      reject(error);
    };
  });
  return valid_to;
};

(async () => {
  let a = await main();
  console.log(a);
  a = await main();
  console.log(a);
})();

Update

Here I try withiy try/catch, but get

TypeError: https.request(...).then is not a function

error.

async function main() {
  options.agent = new https.Agent(options);
  const valid_to = await new Promise((resolve, reject) => {

    const req = https.request({
      ...options, checkServerIdentity: function (host, cert) {
        resolve(cert.valid_to);
      }
    }).then(response => {
      req.end();
    }).catch(rej => {
      reject(rej);
    });
  });
  return valid_to;
};

Update 2

Here the promise is moved inside the try block, but I get same error.

async function main() {
  options.agent = new https.Agent(options);
  try {
    const valid_to = await new Promise((resolve, reject) => {
      const req = https.request({
        ...options, checkServerIdentity: function (host, cert) {
          resolve(cert.valid_to);
        }
      });
      req.end();
    });
    return valid_to;
  } catch (error) {
    reject(error);
  };
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

request is a stream, so you should register error listener there, reject, and then catch the error:

async function main() {
  options.agent = new https.Agent(options);
  const valid_to = await new Promise((resolve, reject) => {

      const req = https.request({
        ...options, checkServerIdentity: function (host, cert) {
          resolve(cert.valid_to);
        }
      }).on('error', (error) => {
        console.error(error);
        reject(error);
    });
      req.end();

  });
  return valid_to;
};

(async () => {
  let a = await main().catch(err=>console.log(err));
  console.log(a);
})();
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