V
V
Vladislav2016-07-24 12:24:01
JavaScript
Vladislav, 2016-07-24 12:24:01

What is the best way to handle promise/callback responses?

There is a regular js server that makes a request to the database, after the response of which it makes more requests to the database, etc. until it does the right amount, and then returns the response to the browser. Actually, the question is how best to process each subsequent request to the database, because it turns out to be a very nested code.

router.route('/:user')
    .get(function (req, res) {
        var user= req.params.user;
        userService.findUser(productName, function(err, data) {
             if (err) 
                 res.json(err);
             else  //и да тут еще проблема, почему не передается data.object и data.user??
                 providerService.findProvider(data.object, data.user, function(err, data) {
                     // и т.д. получаем data и идем искать в базу
                 });
        });
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Zherebko, 2016-07-24
@G_tost

If you use native promises from ec6, then something like
new Promise(...).then(// return get /uses).then(// return get userinfo foreach user in ysers ).then(// send send response to browser)
promises help avoid callbacks and make the code more explicit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question