Harsh Patel
Harsh Patel

Reputation: 6830

Promise chain: parent promise is not waiting until child promise gets executed

I fetch data from one mongoDB collection. In that response I got the Id of another collection's data and fetch that and merge that in one object.

Here's my code. It doesn't wait until the child promise gets executed.

What am I doing wrong?

 Courses.find({})
    .then( course => {
        //getting data from one collection
        let CoursePromises = course.map(
          key => {

            new Promise((resolve, reject) => {
                key.questions = []
                //getting data from another collection via Id fetched from first collection.
                let getQuestionsPromises = key.questionIds.map(
                  ques =>
                    new Promise((resolve, reject) => {
                             Questions.find({_id: ques._id})
                                .then(question => {
                                    resolve(question)
                                }).catch(err => {
                                    console.error("Error in  question ", err.message)
                                })
                    })
                )
                Promise.all(getQuestionsPromises).then((data) => {
                    
                     key.questions.push(data)
                    console.log("getQuestionsPromises", key)
                })
                resolve(key)
            })
        })

        Promise.all(CoursePromises).then((data) => {
            console.log("CoursePromises") // here promise is now wait for exection done 
            res.send({ status: true, data: course })
          }
        ) 

I got first collection response like this:

{
    "status": true,
    "data": [
        {
            "_id": "5e3c1b683ac31f24da39e50a",
            "courseName": "Test",
            "duration": 1,
            "createdBy": "John Die",
            "__v": 0,
            "updatedAt": "2020-02-06T13:58:00.906Z",
            "createdAt": "2020-02-06T13:58:00.906Z",
            "isAssigned": false,
            "questions": []
            "questionIds": [
                {
                    "index": 1,
                    "_id": "5e3c1b683ac31f24da39e509"
                }
            ]
        }
    ]
}

with questionIds I fetch another recoed and put that reseponse in the existing object like this :

{
    "status": true,
    "data": [
        {
            "_id": "5e3c1b683ac31f24da39e50a",
            "courseName": "Test",
            "duration": 1,
            "createdBy": "John Die",
            "__v": 0,
            "updatedAt": "2020-02-06T13:58:00.906Z",
            "createdAt": "2020-02-06T13:58:00.906Z",
            "isAssigned": false,
            "questions": [
                [
                    [
                        {
                            "_id": "5e3c1b683ac31f24da39e509",
                            "index": 1,
                            "isVideo": false,
                            "questionType": "MCQ",
                            "question": "Is this a demo question?",
                            "title": "Question",
                            "description": "this is question description",
                            "link": "",
                            "createdBy": "Harsh",
                            "updatedBy": "",
                            "__v": 0,
                            "updatedAt": "2020-02-06T13:58:00.521Z",
                            "createdAt": "2020-02-06T13:58:00.521Z",
                            "options": [
                                {
                                    "one": "two"
                                }
                            ]
                        }
                    ]
                ]
            ],
            "questionIds": [
                {
                    "index": 1,
                    "_id": "5e3c1b683ac31f24da39e509"
                }
            ]
        }
    ]
}

Upvotes: 1

Views: 535

Answers (1)

Ashh
Ashh

Reputation: 46451

You should follow pure async-await syntax when working with such complex structure. Also use .lean() to convert the course from mongoose object to a plain object.

Simplified code:

const course = await Courses.find({}).lean();
const coursePromises = course.map(async key => {
  key.questions = [];
  const getQuestionsPromises = key.questionIds.map(async ques => {
    const question = await Questions.find({ _id: ques._id });
    key.questions.push(question);
  });
  await Promise.all(getQuestionsPromises)
});
await Promise.all(coursePromises)
return res.send({ data: course })

Upvotes: 2

Related Questions