Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

javascript – working with multiple promises inside loop – how to return data outside of promise?

I’m struggling to understand how I can return data from multiple promises to build up an array of data.

Is there anyway I can return the data outside of the promise to push to the data variable?

I have the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

db_sh.find({
    selector: {sh: req.params.sh_id},
    fields: ['_id', 'sh_id', 'time'],
    sort: ['_id']
}).then(function (result) {
    let data = {};
    console.log('Found: ' + result.docs.length);
    if (result.docs.length > 0) {
        for (var i = 0; i < result.docs.length; i++) {
            let student = result.docs[i];

            Promise
                .all([getMealBooking(student._id), getStudentData(student._id)])
                .then(function(response) {
                    var meal_booking_data = response[0];
                    var student_data = response[1];
                    console.log(meal_booking_data);

                    console.log(student_data);
                })
                .catch(function (err) {
                    return res.send(false);
                });

            data[student.time] = [
                meal_booking_data,
                student_data
            ]
        }
    }
    /** Sort Data Oldest First*/
    data = Object.keys(data).sort().reduce((a, c) => (a[c] = data[c], a), {});
    console.log(data);
    res.send(data);
});

I have two promises (getMealBooking() & getStudentData()): and I am using Promise.all() to return me the results of both of these promises. I have tried to return the data but I cannot get the results to build up the data array.

Any help to be able to build up a list of all my data would be great.

>Solution :

Another Promise.all() is needed for the loop that contains the Promise.all() you’ve already figured out. It’s better to factor a little so you can see what’s happening.

function getStudentMealAndData(student) {
  return Promise
    .all([getMealBooking(student._id), getStudentData(student._id)])
    .then(function(response) {
      var meal_booking_data = response[0];
      var student_data = response[1];
      console.log(meal_booking_data);
      console.log(student_data);
      return { student, meal_booking_data, student_data };
    })
    .catch(function (err) {
      return res.send(false);
    });
}

This simplifies the then block a bit…

}).then(function (result) {
    console.log('Found: ' + result.docs.length);
    let promises = []
    for (var i = 0; i < result.docs.length; i++) {
      let student = result.docs[i];
      promises.push(getStudentMealAndData(student));
    }
    return Promise.all(promises);
}).then(results => {
  // results are an array of [{ student, meal_booking_data, student_data }, ...]
  let data = results.reduce((acc, s) => {
    acc[s.student.time] = [ s.meal_booking_data, s.student_data ];
    return acc;
  }, {});
  data = Object.keys(data).sort().reduce((a, c) => (a[c] = data[c], a), {});
    console.log(data);
    res.send(data);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading