I'm looking to do two things with the Q
library for node.
1) Issue a number of asynchronous queries, each of which uses the result of the previous one, and 2) Have access to the results of every query, all at once, once they're all complete
E.g. let's say a database has ridings, each of which has a city, each of which has a state, each of which has a country. Given a riding, I want to print out all this geographical data, all at once.
var ridingObj = // Already have access to this database object
ridingObj.getRelated('city')
.then(function(cityObj) {
// Have access to cityObj.getField('name');
return cityObj.getRelated('state');
})
.then(function(stateObj) {
// Have access to stateObj.getField('name');
return stateObj.getRelated('country');
})
.then(function(countryObj) {
// Have access to countryObj.getField('name');
// Can't console.log anything but the country, because we no longer have access :(
})
With this pattern I'm getting access to all the data, but not at the same time.
What is considered a clean, conventional pattern for getting all the data at once??