RxJS - 错误处理运算符 retry
如果出现错误,此运算符将负责在源 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('http://localhost:8081/getData').pipe(retry(4)); final_val.subscribe( x => console.log(x), => console.error(err), () => console.log("Task Complete") );
在示例中,我们使用 ajax 调用 url。url − http://localhost:8081/getData 给出 404,因此 retry() 运算符尝试再次调用 url 4 次。输出如下所示