Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!