Skip to content

Instantly share code, notes, and snippets.

@rivertam
Created March 14, 2018 13:51
Show Gist options
  • Save rivertam/f88a9b5c6937964d1ab1b250e88df09a to your computer and use it in GitHub Desktop.
Save rivertam/f88a9b5c6937964d1ab1b250e88df09a to your computer and use it in GitHub Desktop.
Getting count of brands
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');
});
@shraey96
Copy link

Okay thanks! I kinda have started to understand the concept of promises now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment