Promises as function return in Node.js

Asked

Viewed 1,009 times

5

I am using the Sequelize.js. framework to perform queries in the database, the result of the function find returns an object with 3 methods: success, error and complete

Ex.:

models.ItensPedido.findAll( {where : {PedidoId: req.params.pedidoId}})
    .success( function( pedidos ) {
    res.send(200, pedidos);
    next;
})
.error( function(err) {
    console.log("Erro ao listar itens do pedido", req.params, err);
    res.send(500, "Erro ao listar itens do pedido: ", err);
    next();
})

I tried to make a function that returned the same way, but without success

function validateLogin( pemail, ppwd ) {
    return function () {
        var successfull = false;
        var error;
        var user;

        exports.success = function ( userSuccess ) { 
            if (!successful) retrun; 

            userSuccess(user);
        }
        exports.error = function ( userError ) {
            if (!error) {retrun;} 
            userError(error);
        }

    models.User.find( { where: Sequelize.and({email: pemail}, {pwd: ppwd}) })
        .success( function (foundUser) {
           if (!user) {error = "User not found."; return;}
           user = foundUser;
           successfull = true;
        })
        error( function (err) {
           error = err;
        })

    }
}

but I always have as return that the object does not have a method success

the idea is to use it as follows:

validateLogin( "[email protected]", "123456")
    .success( { //logica de sucesso  } )
    .error( { //logica de erro } );

today I get the expected result as follows, passing the callbacks to the method:

validateLogin( "[email protected]", "123456", doOnSuccessLogin, doOnErrorLogin);

but for this I need the two callbacks to be declared within the same function by the closure of the parameters of the restify to answer the Rest call

How to return an object, asynchronously, with methods after the execution of the query, in nodejs?

1 answer

3


There are some parts of your code that I don’t quite understand, but to answer the question, return a object and not a function:

function validateLogin( pemail, ppwd ) {
    return {
       success: function() {},
       error: function() {}
    };
}

Considering your update, I suggest returning your own "promise" (or whatever Sequelize implemented there) returned by models.User.find. Thus:

function validateLogin( pemail, ppwd ) {
    return models.User.find( { where: Sequelize.and({email: pemail}, {pwd: ppwd}) });
}

// USO:

validateLogin( "[email protected]", "123456")
    .success( function(foundUser) {  } )
    .error( function(err) { } );
  • I changed the title. After your last post I saw that the sequelize really uses Promises.

  • Was there anything left to explain? I didn’t quite understand the reward!

  • I tried to find an explanation of how to use the predecessors myself, so I wanted more visibility in the question. But then I found an online course on the site http://nodeschool.io/. But still your answer helped me solve the problem. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.