Created
March 14, 2018 13:51
-
-
Save rivertam/f88a9b5c6937964d1ab1b250e88df09a to your computer and use it in GitHub Desktop.
Getting count of brands
This file contains hidden or 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
categorySchema.find({}) | |
.exec() | |
.then(categories => { | |
// When you return a promise from a .then callback, the promise that gets returned from the original .then | |
// resolves when the inner promise that you return resolves | |
return Promise.all(categories.map(cat=> { | |
return brandSchema.find({category: cat._id}).count() | |
})); | |
}) | |
.then(counts => { | |
// so this resolves when the Promise.all above resolves | |
// counts is an array of numbers. I don't know what you want to do with them. | |
console.log('DONE'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay thanks! I kinda have started to understand the concept of promises now 👍