Yong
Yong

Reputation: 1127

how to call http request immediately again after completion

This HTTP call is long polling. My current solution is:

interval(5000).subscribe(() => {
    if (!this.isLoading) {
        this.isLoading = true;
        this.httpService.getEvents()
          .pipe(finalize(() => this.isLoading = false))
          .subscribe((r) => {
          // do something
        });
      }
    });

But I want to reduce the latency when the last http call is done. How to call this HTTP request in finalize again at once?

Upvotes: 0

Views: 214

Answers (1)

martin
martin

Reputation: 96889

It looks like you could use switchMap to cancel any pending requests (requests that take more than 5s) or exhaustMap to wait until the active request completes while ignoring any subsequent emissions from interval (it depends on what you want to achieve).

timer(0, 5000)
  .pipe(
    exhaustMap(() => this.httpService.getEvents()),
    take(1),
    repeat(),
  )
  .subscribe(console.log)

Upvotes: 3

Related Questions