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

311
Visualizações
How do I return from .on() in function in javascript?

I have a problem where I want to return a error (if there is one) and then print it.
I have a main.js file with:

const webserver = require('./webserver.js');
webserver.start();
//Here i want to print the error if there is one

and a module named webserver.js:

var webserver = {
    start: async function () {
        let serv = server.listen('a', () => { //<- "a" would give an error
            console.log(`Listening at http://localhost:${port}`)
        }).on('error', function (err) {
            return {'num': 403,'err': err}; //<- this doesn't work
        });
    }
}
module.exports = webserver;

How do I have to change my code so that I can return the error if there is one and receive it in my main.js file?

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

0

Solved it like this:

start: function () {
    return new Promise((respond, reject) => {
        serv = server.listen(config.port, () => {
            console.log(`Listening at http://localhost:${port}`)
        }).on('error', function (err) {
            respond({
                'num': 403,
                'err': err
            });
        });
    })
}
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