gpt4 book ai didi

javascript - Rxjs 可观察等待直到满足某些条件

转载 作者:数据小太阳 更新时间:2023-10-29 04:20:09 24 4
gpt4 key购买 nike

我有以下重试逻辑来重试操作。它适用于单个请求。对于多个正在进行的请求,我想在重试之前等待现有的重试逻辑完成。

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);
}

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

最佳答案

您可以将 async/await 用于 Promise resolve 的目的:

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);
}

关于javascript - Rxjs 可观察等待直到满足某些条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45754771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com