Rxjs Retry with Delay function

前端 未结 10 2393
忘了有多久
忘了有多久 2020-11-30 06:32

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

10条回答
  •  余生分开走
    2020-11-30 06:53

    Works on rxjs version 6.3.3

    https://stackblitz.com/edit/http-basics-8swzpy

    Open Console and see the retries

    Sample Code

    import { map, catchError, retryWhen, take, delay, concat } from 'rxjs/operators';
    import { throwError } from 'rxjs';
    
    
    export class ApiEXT {
    
        static get apiURL(): string { return 'http://localhost:57886/api'; };
        static httpCLIENT: HttpClient;
    
     static POST(postOBJ: any, retryCOUNT: number = 0, retryINTERVAL: number = 1000) {
            return this.httpCLIENT
                .post(this.apiURL, JSON.stringify(postOBJ))
                .pipe(
                    map(this.handleSUCCESS),
                    retryWhen(errors => errors.pipe(delay(retryINTERVAL), take(retryCOUNT), concat(throwError("Giving up Retry.!")))),
                    catchError(this.handleERROR));
        }
    
    
      private static handleSUCCESS(json_response: string): any {
            //TODO: cast_and_return    
            return JSON.parse(json_response);
    
        }
    
     private static handleERROR(error: Response) {
            let errorMSG: string;
            switch (error.status) {
                case -1: errorMSG = "(" + error.status + "/" + error.statusText + ")" + " Server Not Reachable.!"; break;
                default: errorMSG = "(" + error.status + "/" + error.statusText + ")" + " Unknown Error while connecting with server.!"; break;
            }
            console.error(errorMSG);
            return throwError(errorMSG);
        }
    
    }
    

提交回复
热议问题