Angular RXJS forkJoin completion progress

ぃ、小莉子 提交于 2020-08-08 06:39:05

问题


I would like to have a progress counter for bulk HTTP requests in my Angular app. I use forkJoin to execute an array of Observables. The pipe only executes once. It does not execute for the X number of requests in the obs variable. Is there a different way to get a complete Observable in a forkJoin?

Here is what I have tried:

    let obs: Observable<any>[] = [...];
    let counter: number = 0;
    // obs has 5 items here.
    forkJoin(obs)
    .pipe(
      tap(() => {
        counter++;
      })
    )
    .subscribe(res => {
    })
    .add(() => {
      loadingRef.close(loadingRef);
      this.refresh();
      // Counter only equals 1 here.
      // It should equal 5.
    });

回答1:


You can transform the observables by adding a pipe, like this:

forkJoin(this.obs.map(o => o.pipe(tap(() => this.count++))))
    .subscribe();

Well, the count goes for a very bumpy ride, but it's one solution

See the stackblitz



来源:https://stackoverflow.com/questions/58328087/angular-rxjs-forkjoin-completion-progress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!