英文:
Can't catch the error from BehaviorSubject, or any subject in rxjs 7
问题
以下是您要翻译的内容:
"The idea is to catch the error message Next err =================>
thrown after 6 sec. In console log we can see only first thrown error, which is very strange behavior.
Here is link in stakblitz. https://stackblitz.com/edit/typescript-tdpwu3?file=index.ts
interval,
of,
throwError,
timer,
ReplaySubject,
Subject,
BehaviorSubject,
} from 'rxjs';
import { mergeMap, retry, tap } from 'rxjs/operators';
//emit value every 1s
let source = new Subject();
const example = source.pipe(
tap((ob) => console.log('OBJ: ', ob)),
//retry 2 times on error
retry({
delay: (errors) => {
console.log('Last error : ', errors);
return timer(3000);
},
})
);
setTimeout(() => {
source.error({ message: 'Error here !!!' });
}, 3000);
setTimeout(() => {
console.log('Next err =================>');
//source = new BehaviorSubject({ status: 'open' });
source.error({ message: 'Next err =================>' });
}, 6000);
const subscribe = example.subscribe();
source.next({ status: 'open' });
英文:
The idea is to catch the error message Next err =================>
thrown after 6 sec. In console log we can see only first thrown error, which is werry strange behavior.
Here is link in stakblitz. https://stackblitz.com/edit/typescript-tdpwu3?file=index.ts
import {
interval,
of,
throwError,
timer,
ReplaySubject,
Subject,
BehaviorSubject,
} from 'rxjs';
import { mergeMap, retry, tap } from 'rxjs/operators';
//emit value every 1s
let source = new Subject();
const example = source.pipe(
tap((ob) => console.log('OBJ: ', ob)),
//retry 2 times on error
retry({
delay: (errors) => {
console.log('Last error : ', errors);
return timer(3000);
},
})
);
setTimeout(() => {
source.error({ message: 'Error here !!!' });
}, 3000);
setTimeout(() => {
console.log('Next err =================>');
//source = new BehaviorSubject({ status: 'open' });
source.error({ message: 'Next err =================>' });
}, 6000);
const subscribe = example.subscribe();
source.next({ status: 'open' });
答案1
得分: 1
我在这里没有看到奇怪的行为。一旦源可观察对象发出第一个错误 { message: 'Error here !!!' }
,它就会关闭。
来自文档:
如果传递了错误或完成通知,那么之后将不会传递任何其他内容。
第二个错误 { message: 'Next err =================>' }
从未被发送到可观察对象。因此,在retry
内部永远不会看到第二个错误。
英文:
I don't see strange behavior here. The source observable is closed once it emits the first error { message: 'Error here !!!' }
.
From the docs:
> If either an Error or Complete notification is delivered, then nothing else can be delivered afterwards.
The second error { message: 'Next err =================>' }
is never emitted to the observable. So the second error is never seen inside the retry
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论