I come from an object orientated background and the following would be my ideal syntax for combining the attributes of two or more models in a json response with Express:
//Verbose
app.get('/boat_house', function(req, res){
var boat = Model1;
var house = Model2;
var bColour = boat.colour;
var hWidth = house.width;
res.jsonp({
boatColour: bColour,
houseWidth: hWidth,
});
});
//Compact
app.get('/boat_house', function(req, res){
res.jsonp({
boatColour: Model1.colour,
houseWidth: Model2.width,
});
});
From what I have seen, this is not possible. I have looked into fibers and async and I understand that Node is full of many modules to solve many problems. Though I have ended up chasing my tail while trying to emulate the above.
- How do I combine attributes from Model1 and Model2 into res.jsonp?
- What anti callback hell module best emulates the above syntax?
- Am I missing an epiphany? Do I need to let go of my OO ways and understand how to solve my problems (namely the one above) in a functional/modular manner?
EDIT:
The models are retrieved from a datastore. For example with the mongoose API you would retrieve Model1 by:
Boat.findOne(function(boat){
//do something with boat
});
I have come across this similar question, the answer to which suggests the use of async.parallel. I would prefer a syntax similar to the following:
var when = require('HypotheticalPromiseModule').when;
var boat = Model1.getAsync();
var house = Model2.getAsync();
when(boat, house).then(function() {
res.jsonp({ ... });
});
Is there an npm module out there that would give me this?