超时的角度HttpClient似乎无济于事

时间:2018-11-07 14:00:59

标签: angular angular-httpclient

这就是我正在执行请求的方式(并且正在运行)。

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

现在,我有一个特定的地址,响应时间太长,因此我收到504。

所以我正在尝试使用timeout,但似乎没有什么变化,就像在Chrome上最多等待1分钟一样。代码如下:

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

我可能做错了什么?

与之合作:

角度6.1.0

1 个答案:

答案 0 :(得分:0)

您可以尝试使用takeUntil方法来代替超时

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;
  });
相关问题