Skip to content

Instantly share code, notes, and snippets.

@PistachioPony
Created February 19, 2014 22:49
Show Gist options
  • Save PistachioPony/9103314 to your computer and use it in GitHub Desktop.
Save PistachioPony/9103314 to your computer and use it in GitHub Desktop.
Change code to use Async Parallel
// Here mariaItem is called first and then moves onto mariaView.
// mariaView has two callbacks happening asynchronously but they need the info from mariaItem first.
var mariaItem = function (req, res, next) {
Item.findOne({ _id: "5099803df3f4948bd2f98391"}, function (err, result){
req.contents = {};
req.contents.result = result;
next();
});
};
var mariaView = function (req, res, next){
async.parallel([
function(callback){
User.findOne({ _id: req.contents.result.seller.sellerId}, function (err, result) {
req.contents.seller = result;
callback();
});
},
function(callback){
Group.find({ _id: {$in: req.contents.result.groups}}, function (err, result){
req.contents.groups = result;
callback();
});
}], function(err, something){
res.json(req.contents);
});
};
app.get('/maria/item',
mariaItem,
mariaView
//mariaTwo,
//mariaS,
//mariaG
// mariaShowIt
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment