I have the following code and I would like to avoid nested callbacks:
app.get '/performers', (req, res) ->
conductor = require('models/conductor').init().model
soloist = require('models/soloist').init().model
orchestra = require('models/orchestra').init().model
chamber = require('models/chamber').init().model
performers = {}
conductor.find {}, (err, result) ->
performers.conductor = result
soloist.find {}, (err, result) ->
performers.soloist = result
orchestra.find {}, (err, result) ->
performers.orchestra = result
chamber.find {}, (err, result) ->
performers.chamber = result
res.json performers
Any ideas?
I find the
async
library to be a cleaner solution than promises for this sort of thing. For this specific case,async.parallel
would work well.I'm not overly familiar with coffeescript, but it would look something like this:
You can also organize your code like this: