Created
October 30, 2022 13:35
-
-
Save NetanelBasal/2b8f620a84bd5d7a15825d0bd608923c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
}, | |
}) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :-)