I am trying to use retry with delay function, I expect function will call after 1000ms delay, but it doesnot, what can be error here?
look at conso
All of this is RxJS 6+
You could use the fully tested operator from this package, or scroll down to see the source :)
npm i rxjs-boost
import { retryWithDelay } from 'rxjs-boost/operators';
obs$.pipe(
// will retry 4 times with a 1s delay before each try:
retryWithDelay(1000, 4)
);
As most (or maybe none) of the other answer didn't meet all of my criteria, I'll list my solution below. Goals:
x times if an error is thrown. ✅y before each retry. ✅strict: true – but this was quite difficult to mess up. ✅As every other answer we'll use the retryWhen operator to catch the errors. To track the amount of repetitions can use the scan operator. To limit the amount of repetitions we'll simply throw an error inside a map operator.
The original source uses throwIf, but in that case we could simply use retryWithDelay from rxjs-boost.
Last we'll use the delay operator to add the delay between the different executions:
import { MonoTypeOperatorFunction } from 'rxjs';
import { delay as delayOperator, map, retryWhen, scan } from 'rxjs/operators';
export function retryWithDelay(
delay: number,
count = 1
): MonoTypeOperatorFunction {
return (input) =>
input.pipe(
retryWhen((errors) =>
errors.pipe(
scan((acc, error) => ({ count: acc.count + 1, error }), {
count: 0,
error: undefined as any,
}),
map((current) => {
if (current.count > count) {
throw current.error;
}
return current;
}),
delayOperator(delay)
)
)
);
}