重试,然后忽略错误并获取源值 rxjs

问题描述

错误或没有错误,我想重试后进入订阅成功处理程序。要进行测试,请注释掉 setTimeout 行。这将使 checkforText$ 始终抛出错误。我想忽略最后的错误,仍然将编辑器返回到订阅成功处理程序。

https://codepen.io/Spankid/pen/gOgVZEE

var editor = { innerText: "" }
var editorOpened$ = of(editor) // observe editor object

function checkforText(){
    return new Promise((resolve,reject) => {
        console.log('checking for text',editor)
    
        if (editor.innerText == ""){
            reject('No text');
        } else {
            resolve(editor)
        }
    })
}

var checkForText$ = defer( () => from(checkforText())) // observe text on editor object

// comment out this line out to test
setTimeout( _ => { editor.innerText = 'testing' },2000) 

editorOpened$.pipe( 
    switchMap(editor => 
        checkForText$.pipe(
            retryWhen(errors => {
                console.log ('no text found... retrying')
                return errors.pipe( 
                    delay(1000),take(3),)
            }),)
     )  
).subscribe(editor => {
    console.log('FINISH CHECKING',editor)  
},err => console.log('ERROR ',err))
    

找到文本时输出(setTimeout 未注释掉):

checking for text {innerText: ""}
no text found... retrying
checking for text {innerText: ""}
checking for text {innerText: "testing"}
FINISH CHECKING {innerText: "testing"}

未找到文本时的目标输出(注释掉 setTimeout):

checking for text {innerText: ""}
no text found... retrying
checking for text {innerText: ""}
checking for text {innerText: ""}
checking for text {innerText: ""}
FINISH CHECKING {innerText: ""}

未找到文本时的实际输出(注释掉 setTimeout):

checking for text {innerText: ""}
no text found... retrying
checking for text {innerText: ""}
checking for text {innerText: ""}
checking for text {innerText: ""}

我尝试在 retryWhen 之后添加 catchError 以使其成为订阅成功处理程序。但它仍然没有。有什么想法吗?

解决方法

使用 retryWhen 是不可能的。

重试给定尝试后的 retryWhen 运算符 completes(在本例中,由 take(3)delay(1000) 指定)。因此,如果在重试期间,您的源 observable checkForText$ 没有发出已解决的承诺或非错误值,则由 checkForText$ 和 retryWhen 组成的管道将不会发出任何内容,最终流将完成重试后.

此后无法进入成功处理程序,除非您专门让 checkForText$ 在重试期间发出一些非错误值。

所以,如果你在你的观察者中添加一个完整的回调,如下所示,

editorOpened$.pipe( 
    switchMap(editor => 
        checkForText$.pipe(
            retryWhen(errors => {
                console.log ('no text found... retrying')
                return errors.pipe( 
                    delay(1000),take(3),)
            }),)
     )  
).subscribe(editor => {
    console.log('FINISH CHECKING',editor)  
  },(err) => console.log('ERROR ',err),() => console.log('I'm done!') //The complete handler
);

当没有找到带有 setTimeout 注释的文本时,您的情况将返回以下内容 -

checking for text {innerText: ""}
no text found... retrying
checking for text {innerText: ""}
checking for text {innerText: ""}
checking for text {innerText: ""}
I'm done!

然而,您可以使用 retry 实现此目的,但我不确定仅使用 retry 会如何引入延迟。

editorOpened$
  .pipe(
    switchMap(editor =>
      checkForText$.pipe(
        retry(3),catchError(errors => of(editor))
      )
    )
  )
  .subscribe(
    editor => {
      console.log("FINISH CHECKING",editor);
    },err => console.log("ERROR ",() => console.log("I'm done!")
  );

这将产生:

checking for text {innerText: ""}
checking for text {innerText: ""}
checking for text {innerText: ""}
checking for text {innerText: ""}
FINISH CHECKING {innerText: ""}
I'm done!
,

正如上面的帖子所指出的,它不可能使用 retryWhen。您但是可以使用重试来实现这一点,确保您的源可观察对象处理任何延迟。

editorOpened$
  .pipe(
    mergeMap(editor =>
      checkForText$.pipe(
        retry(3),catchError(errors => of(editor))
      )
    )
  )
  .subscribe(
    editor => console.log("Finish",editor);,err => console.log("Error",_ => console.log("Complete")
  );

源可观察

function checkforText(){
    return new Promise((resolve,reject) => {
        console.log('checking for text',editor)
        if (editor.innerText == ""){
            setTimeout(_=> reject('No text'),500); // reject after half a second
        } else {
            resolve(editor)
        }
    })
}

var checkForText$ = defer(_=> from(checkforText())) 

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...