Skip to content

Instantly share code, notes, and snippets.

@jl91
Created May 23, 2024 04:04
Show Gist options
  • Save jl91/85f78d43ab979f74f098b82e8904d7e5 to your computer and use it in GitHub Desktop.
Save jl91/85f78d43ab979f74f098b82e8904d7e5 to your computer and use it in GitHub Desktop.
this.petService.getPetById(1)
.pipe(
retryWhen(errors => errors.pipe(
mergeMap((error: HttpErrorResponse) => {
if (error.status === 404) {
return this.authService.refreshToken().pipe(
tap(newToken => {
if (newToken) {
this.token = newToken;
}
}),
catchError(refreshError => {
// Se a renovação falhar, propaga o erro original
return throwError(error);
})
);
}
// Para qualquer outro erro, propaga o erro
return throwError(error);
}),
delay(1000), // Delay opcional entre tentativas
scan((retryCount, error) => {
if (retryCount >= 3) {
throw error;
} else {
return retryCount + 1;
}
}, 0),
)),
)
.subscribe((data) => {
console.log(data);
},
(error) => {
console.error(error);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment