Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffdonthemic/10459873 to your computer and use it in GitHub Desktop.
Save jeffdonthemic/10459873 to your computer and use it in GitHub Desktop.
Node Promises in parallel with mongo
var allPromise = Q.all([ getTemplates(), getGroups() ])
allPromise
.then(function (data) {
console.log(data);
});
function getTemplates () {
var deferred = Q.defer()
Template.find({}, function (err, data) {
if (err) deferred.reject(err) // rejects the promise with `er` as the reason
else deferred.resolve(data) // fulfills the promise with `data` as the value
})
return deferred.promise // the promise is returned
}
function getGroups () {
var deferred = Q.defer()
Group.find({}, function (err, data) {
if (err) deferred.reject(err) // rejects the promise with `er` as the reason
else deferred.resolve(data) // fulfills the promise with `data` as the value
})
return deferred.promise // the promise is returned
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment