0

I am doing recursive call inside for loop, how to know when all calls are done using observables or another technic? The following code is working but I know that there is a better way to do it.

    let assync_service_lenght = groups.length;
    let assync_count = 0;
    groups.forEach(group=> {
      //get contacts from groups
        this.serviceGroups.getGroupsPeople(group.value).subscribe(res => {
            //ADD NEW CONTACT

            assync_count++
            if(assync_service_lenght==assync_count){
              this.sendHTTP(sms);
            } 
      });
    });
  • Possible duplicate of [Promise.all behavior with RxJS Observables?](https://stackoverflow.com/questions/35608025/promise-all-behavior-with-rxjs-observables) – Igor Dec 19 '18 at 18:05
  • Similar to https://stackoverflow.com/questions/35676451/observable-forkjoin-and-array-argument – Igor Dec 19 '18 at 18:05
  • Thanks, @Igor I could not find an answer. It was my first question on Stack. Merry Christmas. – Rodrigo Martins Alves Dec 21 '18 at 20:30

1 Answers1

0

Try something like this:

from(groups)
  .pipe(mergeMap(group => this.serviceGroups.getGroupsPeople(group.value)))
  .subscribe(
    () => console.log('add new contact'),
    e => console.error('error', e),
    () => {
      this.sendHTTP(sms);
    }
  );