[RxJS] Retry with increasing time
with Time Rxjs Retry Increasing
2023-09-14 09:00:45 时间
import { Observable, throwError, timer } from 'rxjs'; import { mergeMap, finalize } from 'rxjs/operators'; export const genericRetryStrategy = ({ maxRetryAttempts = 3, scalingDuration = 1000, excludedStatusCodes = [], }: { maxRetryAttempts?: number; scalingDuration?: number; excludedStatusCodes?: number[]; } = {}) => (attempts: Observable<any>) => { return attempts.pipe( mergeMap((error, i) => { const retryAttempt = i + 1; // if maximum number of retries have been met // or response is a status code we don't wish to retry, throw error if (retryAttempt > maxRetryAttempts || excludedStatusCodes.find((e) => e === error.status)) { return throwError(error); } console.log(`Attempt ${retryAttempt}: retrying in ${retryAttempt * scalingDuration}ms`); // retry after 1s, 2s, etc... return timer(retryAttempt * scalingDuration); }), finalize(() => console.log('We are done!')) ); };
USAGE:
const careTeams$: Observable<any[]> = getMockData([]).pipe( map((results) => { if (result.length == 0) { throw results; } return results; }), retryWhen( genericRetryStrategy({ maxRetryAttempts: 10, scalingDuration: 100, excludedStatusCodes: [500], }) ), catchError((err) => of([])) );
相关文章
- Struts2.5以上版本There is no Action mapped for namespace [/] and action name [userAction_login] associated with context path []
- [Unit testing RxJS] Test complex logic with time progression syntax
- [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
- [Javascript] 4 ways to remove duplicates elements from an array with Javascript
- [PWA] Cache JSON Data in a React PWA with Workbox, and Display it while Offline
- [React] Update Application State with React Apollo ApolloConsumer Component
- [Angular] Configure an Angular App at Compile Time with the Angular CLI
- [AngularFire2] Update multi collections at the same time with FirebaseRef
- [React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD
- [React] Unit Testing with React and webpack
- Efficient and Accurate Arbitrary-Shaped Text Detection with Pixel Aggregation Network(利用像素聚合网络进行高效准确的任意形状文本检测)
- not defined in file libmysqlclient.so.18 with link time reference
- [Typescript] Creating Chainable Method Abstractions with Generics and the Builder Pattern - 05
- [AWS] Creating a Private Hosted Zone and Associating It with Multiple VPCs
- [Prostgres] Select Grouped and Aggregated Data with SQL
- [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
- [Angular] Working with FormArray
- [React] Linting React JSX with ESLint (in ES6)
- Anomaly Detection for Time Series Data with Deep Learning——本质分类正常和异常的行为,对于检测异常行为,采用预测正常行为方式来做
- 《论文阅读》 ProphetChat: Enhancing Dialogue Generation with Simulation of Future Conversation
- Improving Few-Shot Learning with Auxiliary Self-Supervised Pretext Tasks(论文解读)