我具有以下重试逻辑来重试操作。它可以很好地处理单个请求。对于进行中的多个请求,我想等待现有的重试逻辑完成后再重试。

handleError(errors: Observable<any>) {

    const retryCountStart: number = 1;

    // wait if there is any existing operation retrying
    // once it is complete, continue here

    return errors
        .mergeScan<any, any>(
        (retryCount: any, err: any) => {

            if (retryCount <= 5) {
                return Observable.of(retryCount + 1);
            }

        },retryCountStart)
        .delay(1000);
}

如何在上述方法中满足某些条件之前增加延迟?

最佳答案

为此,您可以使用Promise解决方案使用async/await:

async handleError(errors: Observable<any>) {

    const retryCountStart: number = 1;

    // wait if there is any existing operation retrying
    // ----------------------------------------------------------
    await new Promise(resolve => {
        // declare some global variable to check in while loop
        while(this.retrying){
            setTimeout(()=> {
                // Just adding some delay
                // (you can remove this setTimeout block if you want)
            },50);
        }

        // when while-loop breaks, resolve the promise to continue
        resolve();
    });
    // ----------------------------------------------------------

    // once it is complete, continue here

    return errors
        .mergeScan<any, any>(
        (retryCount: any, err: any) => {

            if (retryCount <= 5) {
                return Observable.of(retryCount + 1);
            }

        },retryCountStart)
        .delay(1000);
}

10-06 11:56