简体   繁体   中英

Several async calls in the for-loop, need to know when all calls are done using observables

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM