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

148
Views
Error chain in NodeJs with sychronize

I wrote a function to create a user profile.

I use the synchronise module to get rid of the callback hell.

When I do this, everything works fine:

    var sql = jsonSql.build({
        type: 'insert',
        table: 'tbl_user',
        values: {firstname: data.firstname, lastname: data.lastname, email: data.email, website: data.website, password: data.hashedPassword},
    });

    try {
        var query = sync.await(db.query(sql.query, sql.values, sync.defer()));
    } catch (err) {
        res.status(500).send(err);
        return;
    }

When sql finds a user with an already used mail address it sends an error. Perfect.

Then I thought it would be a good idea to separate the code where the user is written into the database, because maybe you want to create a user in the admin-backend and then I could reuse the smaller function.

The smaller function:

function createUserInDB(data) {

    var sql = jsonSql.build({
        type: 'insert',
        table: 'tbl_user',
        values: {firstname: data.firstname, lastname: data.lastname, email: data.email, website: data.website, password: data.hashedPassword},
    });

    return db.query(sql.query, sql.values);
}

Which now gets called from the main function:

try {
        var query = sync.await(userService.createUserInDB(data, sync.defer()));
        console.dir(query);
    } catch(err) {
        console.dir(err);
        res.status(500).send(err);
        return;
    }

This does not work.

How can I get the result of the smaller function (as error or query result) in my main function, so I am able to use try / catch?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I finally solved it.

function createUserInDB(data, resolve) {
sync.fiber(function() {

    data.hashedPassword = bcrypt.hashSync(data.password, 10);

    var sql = jsonSql.build({
        type: 'insert',
        table: 'tbl_user',
        values: {firstname: data.firstname, lastname: data.lastname, email: data.email, website: data.website, password: data.hashedPassword},
    });

    try {
        var query = sync.await(db.query(sql.query, sql.values, sync.defer()));
        //first argument would be error
        resolve(null, query);
    } catch(err) {
        resolve(err);
    }

});
}

Basically, add the callback function to the function parameter and then just resolve it inside the fiber.

over 4 years ago · Santiago Trujillo 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!