1

I have a function that return data, called medicalService.getMedicalEvents(). Like this i catch all medical events, and now for each medical event i need to pass the id to another function to catch all the attendees of that event. medicalService.getMedicalAttendees(idMedicalEvent)

when i got all the attendees for each event i need to repeat the process to get all the targets. So for each attendee i will the function medicalService.getTarget(accountId), accountId is the attendeeId.

when i get all the targets i need to do another for each to create one object with all the information that i need. Like the eventName from first forEach, attendeeName from second forEach and target third forEach.

My question is related to the promises, how can i with promises be able to return an object created like that call.mainObj?

medicalService.getMedicalEvents().then(function(resp) {
      call.medicalEvents = resp.data;
      call.numberEvents = call.medicalEvents.length;

      call.medicalEvents.forEach(function(event) {
          var deferred = $q.defer();
          var idMedicalEvent = event.Id.value

          medicalService.getMedicalAttendees(idMedicalEvent).then(function(resp2) {
            call.allAttendeesForEvent = resp2.data;

            call.allAttendeesForEvent.forEach(function(attendee) {
              var accountId = attendee.Account_vod__c.value;

              medicalService.getTarget(accountId).then(function(resp3) {
                call.targetInfo = resp3.data;

                call.targetInfo.forEach(function(target) {
                  var targetName = "C";
                  if (target.SRV_INT_Team_target__c.value !== undefined && target.SRV_INT_Team_target__c.value !== "") {
                    targetName = target.SRV_INT_Team_target__c.value;
                  }
                  call.mainObj.push({
                    eventName: event.Name.value,
                    attendee: attendee.Name.value,
                    target: targetName,
                    speciality: attendee.SRV_Specialty__c.value
                  });
                })
              })
            })
          })
          medicalEventsDeferred.resolve(resp);
        },
        function(resp) {
          medicalEventsDeferred.reject(resp);

        })
JLRishe
  • 99,490
  • 19
  • 131
  • 169
Leo
  • 21
  • 1
  • 4
  • Possible duplicate of [setting a variable to get return from call back function using promise](https://stackoverflow.com/questions/22536385/setting-a-variable-to-get-return-from-call-back-function-using-promise) – Michael Jun 02 '18 at 11:47
  • Avoid the [deferred antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jun 02 '18 at 12:25
  • Use `map` instead of `forEach` – Bergi Jun 02 '18 at 12:25
  • With the `{eventName, attendee, target, speciality}` objects constructed in the innermost loop, they will contain lots of redundant data, which may be what you want but `{eventName, attendees}` (where `attendees` is Array of `{name, speciality, targets}` objects) would be more economical and would reflect the original data relationships. – Roamer-1888 Jun 04 '18 at 10:43

0 Answers0