Last active
August 29, 2015 13:59
-
-
Save jeffdonthemic/10459873 to your computer and use it in GitHub Desktop.
Node Promises in parallel with mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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