I have a situation in my Node (express) controller where I need to check whether a particular language has a parent language, if it does, I need to do a new DB (SQL) call to retrieve the parent language details.
The problem is that this is encouraging a pyramid style (not too far from callback-hell). What if next I need to do another DB call depending on something in the parent language? My code will rapidly become unreadable. I thought promises were meant to solve all this. Is there a better way?
Promise.all(promises)
.then(function(results) {
// check if the language has a macrolanguage, increases execution
// time of api, as queries cannot run in parallel
if (results[0].language.parent_language_id) {
Languages.getParentLanguageDetails(results[0].language.parent_language_id)
//embedded thens are bad style, should be cleaned up
.then(function(newResult) {
results[results.length] = newResult
var data = u.extendResultsObject(results)
res.send(200, data) //success
})
} else {
var data = u.extendResultsObject(results)
res.send(200, data) //success
}
})
push
was a temporary brain-fart. Can you explain what you mean by cache theid
? I'm not sure how to remove the else condition without the second.then
not receiving results if the result doesn't have aparent_language_id
\$\endgroup\$ – Joseph Sep 4 '14 at 0:19