V
V
Vladislav2016-07-24 15:45:33
JavaScript
Vladislav, 2016-07-24 15:45:33

How to handle nodejs request and callback with promise?

There is such a controller function that receives a request from the user, and then work with the data takes place, and at some stage you need to return a response to the user. I figured out how to build data processing in a more or less acceptable way - through the Q library, but I don’t understand how to send a response to the user at some of their stages.

router.route('/:user')
        .get(function (request, response) {
            var userName = req.params.user;
            userService.findUserByName(userName)
                .then(onUserSuccess, errorHandle)
                .then(onProviderSuccess, errorHandle);
    });

function errorHandle(err) {
// здесь мне надо отаправить json пользователю обратно, но для этого мне нужно получить объект reponse //из роута как я могу это сделать. Не передавать же мне этот объект по всей цепочки сервисов?
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2016-07-24
@Div100

router.route('/:user')
        .get(function (request, response) {
            var userName = req.params.user;
            userService.findUserByName(userName)
                .then(onUserSuccess, function (err) {
                        errorHandle(err, reponse);
                })
                .then(onProviderSuccess, function (err) {
                        errorHandle(err, reponse);
                });
    });

function errorHandle(err, reponse) {
    return reponse.status(200).json({
        error: false
    });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question