简体   繁体   中英

How return a value after a forEach loop using Promises?

I need to know how to return a value after a forEach loop using Promises. In this moment, when I launch my main, I get :

[ Promise { <pending> }, Promise { <pending> } ]

(my sampleidlist contains only 2 records) This is my code :

MongoClient.connect("mongodb://127.0.0.1/myproject", function(err, db) {
  return db.collection('RUN').find({
      "idRun": query.idRun
    }).toArray()
    .then((out) => {

      var sampleidlist = out[0].SAMPLE_ID
      var pazlist = []
      // Promisearr is the array of promises where I try to push the promises
      var Promisearr = []
      // there is the function find_paz that return idPaz for every sampleId in sampleidlist                        
      function find_paz(sampleid) {
        // I return a new Promise for every sampleId
        // I want to create an array of idPaz 
        return new Promise((resolve, reject) => {
          db.collection('PATIENTS').find({
              "SAMPLE_ID": sampleid
            }).toArray()
            .then((pazArr) => {
              var singlepaz = []
              singlepaz.push(pazArr[0].idPaz)
              return singlepaz
            })
            .then((singlepaz) => {
              pazlist.push(singlepaz)

            })
        })
      }
      // Here the forEach loop
      sampleidlist.forEach(sampleid => {
        Promisearr.push(
          find_paz(sampleid)
        )
      })
      Promise.resolve(Promisearr)
        .then(Promise.all(Promisearr))
        .then(value => {
          // value return {promise<pending>}
          // I want that value is the array of idPaz
          console.log(value)
        }).catch((err) => {
          console.log('errored', err);
        })

    }).catch((err) => {
      console.log('errored', err);
    })
})

Any suggest? Thank you very much :)

You have it mixed up between Promise.all and Promise.resolve. Here:

return db.collection('RUN').find({ "idRun": query.idRun }).toArray() .then((out) => {

  var sampleidlist = out[0].SAMPLE_ID
  var pazlist = []

  var Promisearr = []

  function find_paz(sampleid) {

      return db.collection('PATIENTS').find({
          "SAMPLE_ID": sampleid
        }).toArray()
        .then((pazArr) => {
          var singlepaz = []
          singlepaz.push(pazArr[0].idPaz)
          return singlepaz
        })
        .then((singlepaz) => {
          pazlist.push(singlepaz)
          return;
        })
    })
  }
  Promise.all(sampleidlist.map(find_paz))
    .then(values => {

      //values is an array with all the promises resolved
      //pazlist should have your data.
    }).catch((err) => {
      console.log('errored', err);
    })

}).catch((err) => {
  console.log('errored', err);
})

Give it a try, let me know if you need clarification or if it doesn't work.

You are using Promise.resolve() and Promise.all() the wrong way. You should just call Promise.all() then .then() , like this :

Promise.all(Promisearr).then(value =>
    console.log(value)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM