Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created October 30, 2022 13:35
Show Gist options
  • Save NetanelBasal/2b8f620a84bd5d7a15825d0bd608923c to your computer and use it in GitHub Desktop.
Save NetanelBasal/2b8f620a84bd5d7a15825d0bd608923c to your computer and use it in GitHub Desktop.
import { repeat } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class MetricsService {
private http = inject(HttpClient);
getMetrics() {
return this.http.get('https://metrics')
.pipe(
repeat({ delay: 30_000 }),
// 👇
retry(3),
// 👇
retry({
count: 3,
delay: 1000
}),
// 👇
retry({
count: 3,
delay: (error, count) => {
// Retry forever, but with an exponential step-back
// maxing out at 1 minute.
return timer(Math.min(60000, 2 ^ count * 1000))
},
})
)
}
}
@fpaaske
Copy link

fpaaske commented Jun 5, 2024

Thanks for a great example (as always), it helped me migrate from retryWhen()!

However, I believe you have a small error on line 24. The ^ operator is bitwise XOR (I think), and you should use Math.pow() instead.
It threw me off looking into this, so I guess it could confuse others as well :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment