π Custom RxJS operators 6/7: "
#angular #rxjs #retryForStatus
The built-in retry operator allows to resubscribe to the source stream once an error has been thrown. It is possible to make a decision whether or not to retry based on the information contained in the error object. In turn, when it comes to streams that represent an http request, the operator can be configured to call an API again only for certain response statuses.
operator
retryForStatus operator"#angular #rxjs #retryForStatus
The built-in retry operator allows to resubscribe to the source stream once an error has been thrown. It is possible to make a decision whether or not to retry based on the information contained in the error object. In turn, when it comes to streams that represent an http request, the operator can be configured to call an API again only for certain response statuses.
operator
interface ErrorWithStatus extends Error {
status: number;
}
interface RetryForStatusConfig {
retryableStatuses: number[];
delay: number;
count?: number;
}
function retryForStatus<T>({
retryableStatuses,
delay,
count = Infinity,
}: RetryForStatusConfig): MonoTypeOperatorFunction<T> {
return retry({
count,
delay: (err: ErrorWithStatus, retryCount) =>
retryableStatuses.includes(err.status)
? timer(retryCount * delay)
: throwError(() => err),
});
}π₯1