重试,然后忽略错误并获取源值 rxjs
Retry, then ignore error and get source value rxjs
错误或没有错误,我想在重试后进入订阅成功处理程序。要进行测试,请注释掉 setTimeout 行。这将使 checkforText$ 始终抛出错误。我想忽略最后的错误,仍然 return 订阅成功处理程序的编辑器。
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)
指定)。因此,如果在重试期间,您的 checkForText$
源可观察对象未发出已解决的承诺或非错误值,则由 checkForText$ 和 retryWhen 组成的管道将不会发出任何内容,最终流将完成 post 重试。
在那之后就没有办法进入成功处理程序,除非你专门让 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 注释的文本,您的情况将 return 跟随 -
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 ", 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!
正如上面 post 指出的那样,它 无法使用 retryWhen。但是,您 可以使用重试 来实现此目的,方法是确保您的源可观察对象处理任何延迟。
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 行。这将使 checkforText$ 始终抛出错误。我想忽略最后的错误,仍然 return 订阅成功处理程序的编辑器。
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)
指定)。因此,如果在重试期间,您的 checkForText$
源可观察对象未发出已解决的承诺或非错误值,则由 checkForText$ 和 retryWhen 组成的管道将不会发出任何内容,最终流将完成 post 重试。
在那之后就没有办法进入成功处理程序,除非你专门让 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 注释的文本,您的情况将 return 跟随 -
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 ", 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!
正如上面 post 指出的那样,它 无法使用 retryWhen。但是,您 可以使用重试 来实现此目的,方法是确保您的源可观察对象处理任何延迟。
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()))