Created
February 19, 2014 22:49
-
-
Save PistachioPony/9103314 to your computer and use it in GitHub Desktop.
Change code to use Async Parallel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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