Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular RXJS forkJoin completion progress

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.
    });
like image 269
CAlex Avatar asked Nov 01 '25 15:11

CAlex


1 Answers

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

like image 155
Eliseo Avatar answered Nov 04 '25 04:11

Eliseo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!