1

I am trying to figure out how to write promise.all for this scenario. Let's say we have a students array. For each student, I need to make a server call for Create and Update in sequence (2 calls). But I can make the calls for all students in parallel. After all the students create and updates are done, I need to make another REST call. Is using promise.all right approach here? Or should sequentially make calls for each student? Below is the code I have, but I don't think it is correct.

    createAndUpdateStudents = () => {
       return this.students.map((student) => {
           return create(student.name) //create returns a promise
                  .then((response) => {
                     return _.get(response, "id");
                  })
                  .then((studentId) => {
                     update(studentId, student.grade) //update returns a promise 
                  });
       });
    }

    save = () => {
       Promise.all(this.createAndUpdateStudents())
       .then(() => {
           //another rest call. This should happen only after all the students are updated
       })
    }

I have written simple promises but I am not able to get this one right.

user281693
  • 615
  • 2
  • 8
  • 20

0 Answers0