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);
})