RxJS - 错误处理操作符重试



如果出现错误,此操作符将负责在源 Observable 上重试,并且重试将基于给定的输入计数。

语法

retry(retry_count: number): Observable

参数

retry_count − 参数 retry_count 是您要重试的次数。

返回值

它将返回带有重试计数逻辑的源 observable。

示例

import { of } from 'rxjs';
import { map, retry } from 'rxjs/operators';
import { ajax } from 'rxjs/ajax';

let all_nums = of(1, 6, 5, 10, 9, 20, 10);
let final_val = ajax('https://:8081/getData').pipe(retry(4));
final_val.subscribe(
   x => console.log(x), => console.error(err),
   () => console.log("Task Complete")
);

在该示例中,我们使用 ajax 调用一个 URL。该 URL − https://:8081/getData 给出了 404,因此 retry() 操作符尝试再次调用该 URL 4 次。输出如下所示

输出

retry Operator
广告
© . All rights reserved.