1

Hello guys here is my code:

function get_group(req, res, next) {
var send_result = function(err, group_list) {
[...]
    res.send(group_list);
    return next();
};

Group.findOne({'_id': req.params._id}, send_result);

}

Now how can I implement the async library (caolan) using async.series and combine the findOne() with send_result, the code as it is look pretty disorganised to me.

EDIT1:

I used this strategy but I am not sure is correct, any suggestion?

function get_group(req, res, next) {
async.waterfall([
    function(callback) {
        Group.findOne({'_id': req.params._id}, callback);
    }
],
function (err, group_list){
    res.send(group_list);
    return next();
});

}

Any suggestion?

4
  • should be something related to async.waterfall, but I still don't succeed with that Commented May 28, 2012 at 17:49
  • I would like to have the function send_result outside so that I could re-use it somewhere else Commented May 28, 2012 at 18:17
  • I just noticed you didn't tag this question with the express tag. If you ain't using express please say so since my answer is based on that assumption. Commented May 28, 2012 at 21:03
  • The question is very open. Your answer look amazing to me, I'm trying to work my question out with your answer Commented May 29, 2012 at 9:31

1 Answer 1

2

For what they call routes in Express.js you actually almost never need to use the async library. The reason is that routes are actually a sort of control flow them self. They take as many middleware as you want so you can divide your routes into small blocks of code.

For example lets say you want to get one record/document from a database do something with it and then send it as json. Then you can do the following:

var getOne = function(req, res, next){
    db.one( 'some id', function(err, data){
        if (err){
            return next( { type: 'database', error: err } );
        };

        res.local( 'product', data );
        next();
    });
};

var transformProduct = function(req, res, next){
    var product = res.locals().product;

    transform( product, function(data){
        res.local('product', data);
        next();
    });
};

var sendProduct = function(req, res, next){
    var product = res.locals().product;
    res.json(product);
};

app.get( '/product', getOne, transformProduct, sendProduct );

If you write middleware for your routes like this you'll end up with small building blocks you can easily reuse throughout your application.

Sign up to request clarification or add additional context in comments.

2 Comments

on restify (based on express) locals() doesn't work, do you have any solution for that?
If you have a question that isn't directly related to your original question you should create a new one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.