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.