0

I am just started learning Promises, i getting console result as [ Promise { pending } ] i want to Print the Exact result coming from the function, can anybody help me on this.

exports.listProjectRepos1 = (req, res)=> {
    let pID = mongoose.Types.ObjectId(req.params.projectId);
    console.log("User Id is ", req.user._id);
    let query = {
        userID: mongoose.Types.ObjectId(req.user._id),
        projectID: pID
    };
        RepositoriesSchema.find(query).lean().then((repos)=> {
            return repos
        }).then((repos)=> {
            let roots = repos.map(exports.populateCodestack1);
            console.log(roots);// trying to Print the Results here
        });
};


exports.populateCodestack1 = function (repo) {
    return new Promise((resolve, reject)=> {
        Promise.all([new Promise((resolve, reject)=> {
            let codeId = repo.codeStack;
            CodeStacksSchema.findOne({ID: codeId}).lean().exec(function (err, codeStack) {
                if (codeStack) {
                    repo.stack = codeStack.name;
                    resolve(repo)
                }
            });
        }),
            new Promise((resolve, reject)=> {
                let owner = mongoose.Types.ObjectId(repo.SCMAccount);
                console.log("Owner Id is", owner);
                ScmaAccount.findOne({_id: owner}).lean().exec(function (err, scm) {
                    if (scm) {
                        repo.type = scm.type;
                        resolve(repo);
                    }
                });
            })

        ]).then(function (result1) {
           // console.log("Refresh Result",result);
            resolve(result1);
        })
    })
};

I want to Print the output of the Function.

Jeevan
  • 756
  • 13
  • 39
  • 1
    Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! Simply `return Promise.all(…)`, don't wrap it in anything. And don't forget to `reject` your promises in case of an error. – Bergi Jan 04 '17 at 12:04

1 Answers1

3

exports.populateCodestack1 returns a Promise, so roots will contain a list a list of Promises.

If you want to wait until all Promises in an array are solved then you would pass it to Promise.all.

RepositoriesSchema.find(query).lean()
  .then( repos => Promise.all(repos.map(exports.populateCodestack1)) )
  .then( roots => {
    console.dir(roots);
  });

beside that: if you use new Promise then you should handle you error cases, and only use new Promise if you need to wrap a function that does not support promises, but never to wrap an existing Promise object:

exports.populateCodestack1 = function(repo) {
  return Promise.all([
    new Promise((resolve, reject) => {
      let codeId = repo.codeStack;
      CodeStacksSchema.findOne({
        ID: codeId
      }).lean().exec(function(err, codeStack) {
        if (err) {
          reject(err);
        } else {
          repo.stack = codeStack.name;
          resolve(repo)
        }
      });
    }),
    new Promise((resolve, reject) => {
      let owner = mongoose.Types.ObjectId(repo.SCMAccount);
      console.log("Owner Id is", owner);
      ScmaAccount.findOne({
        _id: owner
      }).lean().exec(function(err, scm) {
        if (err) {
          reject(err)
        } else {
          repo.type = scm.type;
          resolve(repo);
        }
      });
    })
  ])
};

EDIT

Because the mongoose lib already supports Promises it should be possible to simplify it even more using:

exports.populateCodestack1 = function(repo) {
  return Promise.all([
    CodeStacksSchema.findOne({
      ID: repo.codeStack
    }).lean().then(codeStack => {
      repo.stack = codeStack.name;
      return repo
    }),
    ScmaAccount.findOne({
      _id: mongoose.Types.ObjectId(repo.SCMAccount)
    }).lean().then(scm => {
      repo.type = scm.type;
      resolve(repo);
    })
  ])
};
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • The `.then((repos) => { return repos })` is redundant. – Daniel B Jan 04 '17 at 12:02
  • @DanielB yes, i didn't see that at first, the code is a bit messy. – t.niese Jan 04 '17 at 12:04
  • 1
    @DanielB can u please explain me How its redundant.. I just Stated learning. can you please explain me bit more. – Jeevan Jan 04 '17 at 12:17
  • 2
    @Jeevan Looking at your code `RepositoriesSchema.find(query).lean().then((repos)=> { return repos }).then((repos)` it should be quite obvious that `.lean()` returns the `repos` object to the following `.then()`. So ask why you should have a second `.then()` in the middle of it all that just returns `repos`. It's like writing a function `function(x) { return x; }`. – Daniel B Jan 04 '17 at 12:19