Javascript 重试,然后忽略错误并获取源值rxjs
错误或无错误,我想在重试后进入订阅成功处理程序。要进行测试,请注释掉setTimeout行。这将使checkforText$始终抛出错误。我想忽略最后一个错误,仍然将编辑器返回到订阅成功处理程序 找到文本时输出(setTimeout未注释掉): 未找到文本时的目标输出(注释掉setTimeout): 未找到文本时的实际输出(注释掉setTimeout):Javascript 重试,然后忽略错误并获取源值rxjs,javascript,rxjs,retrywhen,Javascript,Rxjs,Retrywhen,错误或无错误,我想在重试后进入订阅成功处理程序。要进行测试,请注释掉setTimeout行。这将使checkforText$始终抛出错误。我想忽略最后一个错误,仍然将编辑器返回到订阅成功处理程序 找到文本时输出(setTimeout未注释掉): 未找到文本时的目标输出(注释掉setTimeout): 未找到文本时的实际输出(注释掉setTimeout): 我尝试在retryWhen之后添加catchError,希望将其添加到subscribe成功处理程序中。但事实并非如此。有什么想法吗?当 r
我尝试在retryWhen之后添加catchError,希望将其添加到subscribe成功处理程序中。但事实并非如此。有什么想法吗?当
retryWhen
操作符在重试给定尝试后完成
(在这种情况下,由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!
不过,您可以使用重试
来实现这一点,但我不确定仅使用重试
如何引入延迟
editorOpened$
.pipe(
switchMap(editor =>
checkForText$.pipe(
retry(3),
catchError(errors => of(editor))
)
)
)
.subscribe(
editor => {
console.log("FINISH CHECKING", editor);
},
err => console.log("ERROR ", err),
() => 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的是不可能的。不过,您可以通过重试来实现这一点,方法是确保您的source observable处理任何延迟
editorOpened$
.pipe(
mergeMap(editor =>
checkForText$.pipe(
retry(3),
catchError(errors => of(editor))
)
)
)
.subscribe(
editor => console.log("Finish", editor);,
err => console.log("Error", err),
_ => 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()))
能够通过在setTimeout内拒绝承诺引入延迟。在意识到这一点之前,尝试了扩展、repeatWhen和takeWhile等其他方法,可能会延迟拒绝。干杯
editorOpened$
.pipe(
switchMap(editor =>
checkForText$.pipe(
retry(3),
catchError(errors => of(editor))
)
)
)
.subscribe(
editor => {
console.log("FINISH CHECKING", editor);
},
err => console.log("ERROR ", err),
() => 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!
editorOpened$
.pipe(
mergeMap(editor =>
checkForText$.pipe(
retry(3),
catchError(errors => of(editor))
)
)
)
.subscribe(
editor => console.log("Finish", editor);,
err => console.log("Error", err),
_ => 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()))