Angular HttpClient with timeout seems do nothing

Viewed 237

This is how I'm doing my requests (and it's working).

this.http
  .get(url, options)
  .pipe(retryWhen((errors) => this.retry(errors, { delay: retryDelay, take: retryTake })))
  .toPromise()
  .catch((ex) => {
    this.exception(ex);
    throw ex;
  });

Now, I have a specific address that takes too long to respond and because of that I'm getting 504.

So I'm trying work with timeout but it seems nothing changes, like on Chrome it waits only for 1 minute max. Heres the code:

this.http
  .get(`${this.host}/${action}${query}`, options)
  .pipe(timeout(300000), (e) => {
    console.log(e);
    return of(null);
  })
  .pipe(retryWhen((errors) => this.retry(errors, { delay: retryDelay, take: retryTake })))
  .toPromise()
  .catch((ex) => {
    this.exception(ex);
    throw ex;
  });

What could I be doing wrong?

Working with:

Angular 6.1.0

1 Answers

You could try using takeUntil method instead of timeout

import { timer } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

//after 30 seconds, stop retrying
const timer$ = timer(30000);

this.http
  .get(url, options)
  .pipe(retryWhen((errors) => this.retry(errors, { delay: retryDelay, take: retryTake })),
        takeUntil(timer$))
  .toPromise()
  .catch((ex) => {
    this.exception(ex);
    throw ex;
  });
Related