无法捕获来自BehaviorSubject或rxjs 7中任何主题的错误。

huangapple go评论54阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月3日 21:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定