Created
February 19, 2014 22:17
-
-
Save PistachioPony/9102848 to your computer and use it in GitHub Desktop.
Using Async waterfall
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
// SO here I have gone from having to call three different functions to only calling one (mariaView) | |
// I took //mariaItem, | |
//mariaTwo, | |
//mariaS, | |
//mariaG | |
// and wrapped them in one function mariaView that then uses async waterfall. | |
// This will NOT work with async parallel because parallel does all the functions at the same time, and the | |
// second, third and fourth functions need the info from the first function to succeed. | |
var mariaView = function (req, res, next){ | |
async.waterfall([ | |
function(callback){ | |
Item.findOne({ _id: "5099803df3f4948bd2f98391"}, function (err, result){ | |
req.contents = {}; | |
req.contents.result = result; | |
callback(); | |
}); | |
}, | |
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', | |
mariaView | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment