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

What i really want to do with the numbers is actually something like this:
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=> {

// What i really want to do is return an object with the following properties:
return brandSchema.find({category: cat._id}).count()
.exec()
.then((count)=>{

      let categoryInfo = {};
           categoryInfo._id = cat._id;
           categoryInfo.title = cat.title;
           categoryInfo.count = count;

           resultArray.push(categoryInfo)
    })

// then once all results are pushed into the array I want to log "DONE" below.

})); 

})
.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');
});

@rivertam
Copy link
Author

rivertam commented Mar 14, 2018

So what you're asking for is, to a certain extent, simply impossible. It's not that it's impossible to push the results to an array -- it's that it won't matter. You might as well just resolve with the array and then handle it in the callback. It will never be populated synchronously.

What you need is a little less elegant, but inherently no more complicated.

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() 
        // transform the count into the object you want. cat is still in scope, so we can just use it
        // generally, if you want state like categories to continue through asynchronous code, you need to have your Promises
        // resolve with that state so you can access it later. You could use an array outside of the Promise logic, but that's inelegant
        // and harder to work with
        .then(count => {
          return {
            _id: cat._id,
            title: cat.title,
            count,
          };
        });
    })); 
  })
  .then(results => {
    // These results are what you wanted in resultArray
    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