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

149
Visualizações
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 Respostas
Responde à pergunta

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