title: Multiple promises inside multiple forEach js $q
body: <p>I have a function that return data, called <code>medicalService.getMedicalEvents()</code>. Like this i catch all medical events, and now for each medical event i need to pass the <code>id</code> to another function to catch all the attendees of that event. <code>medicalService.getMedicalAttendees(idMedicalEvent)</code></p>
  
  <p>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 <code>medicalService.getTarget(accountId)</code>, accountId is the attendeeId.</p>
  
  <p>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 <code>eventName</code> from first forEach, <code>attendeeName</code> from second forEach and <code>target</code> third forEach.</p>
  
  <p>My question is related to the promises, how can i with promises be able to return an object created like that <code>call.mainObj</code>? </p>
  
  <pre><code>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 &amp;&amp; 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);
  
          })
  </code></pre>