【问题标题】:RxJS Continuous HTTP polling in case it fails as wellRxJS 连续 HTTP 轮询,以防它也失败
【发布时间】:2018-11-22 06:56:41
【问题描述】:

我正在尝试实现 HTTP 轮询。期望每 5 分钟从 HTTP 轮询一次,或者我可以说在服务器上同步。如果失败,它不应该停止主题,而是应该重试 3 次,然后在 5 分钟后再次尝试。万一5分钟后也失败了,应该重试3分钟,场景继续进行。

我尝试的是一些东西。

const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
    .switchMap(() => Rx.Observable.of(axios.get(url)).retry(3));

// .map(response => console.log(`console here !! - ${response}`));
// .concatMap(val => of(`Delayed by: ${val}ms`).pipe(delay(val)));

checkTimeForRestart$.subscribe(
    x => console.log(x),
    error => console.error(`Error: ${error}`),
    () => console.log(`Complete: fires when the observable completes`)
);

如果服务器未启动或存在连接问题,它不应该完成 observable,但应该尝试 3 次,然后在重做该过程后 5 分钟再次尝试。

【问题讨论】:

标签: rxjs


【解决方案1】:

所以问题是,当它重试 3 次时,会向前发送错误通知,从而处理链,这不是您想要的。

一个简单的解决方案是使用catch (catchErrorin RxJS 6) 在 3 次重试后忽略错误:

const checkTimeForRestart$ = Rx.Observable.timer(5 * 60 * 1000, 5000)
  .switchMap(() => Rx.Observable.of(axios.get(url))
    .retry(3)
    .catch(err => Rx.Observable.empty())
  );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    相关资源
    最近更新 更多