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

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

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