RxJS - Error Handling Operator retry



This operator will take care of retrying back on the source Observable if there is error and the retry will be done based on the input count given.

Syntax

retry(retry_count: number): Observable

Parameters

retry_count − The argument retry_count, is the number of times you want to retry.

Return value

It will return back source observable with retry count logic.

Example

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

In the example, we are making a call to a url using ajax. The url − http://localhost:8081/getData is giving a 404 so the retry() operator tries to make a call to url again for 4 times. The output is shown below

Output

retry Operator
Advertisements