RXJS多播在NodeJ和浏览器中不一致的错误行为

发布于 2025-01-31 06:46:46 字数 561 浏览 2 评论 0原文

    const tick$ = interval(1000).pipe(take(10))
    const subject = new Subject<number>()
    tick$.subscribe(subject)

    subject
      .pipe(
        map(x => {
          if (x === 5) {
            throw new Error('error!!!')
          }
          return x
        })
      )
      .subscribe(x => console.log('x1: ', x))

    subject.subscribe(x => console.log('x2: ', x))

上面的代码在浏览器中运行,当x === 5时,只有x1被中断,x2可以继续,但是在nodejs中,X1发生错误杀死了该过程。有人可以解释为什么?先感谢您!

    const tick$ = interval(1000).pipe(take(10))
    const subject = new Subject<number>()
    tick$.subscribe(subject)

    subject
      .pipe(
        map(x => {
          if (x === 5) {
            throw new Error('error!!!')
          }
          return x
        })
      )
      .subscribe(x => console.log('x1: ', x))

    subject.subscribe(x => console.log('x2: ', x))

Above code run in browser, when x === 5, only x1 is interrupted and x2 can continue, but in nodejs, x1 occured error killed the process. Somebody can explain why? Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2025-02-07 06:46:46

在nodejs中,当出现错误时,该过程将被杀死。
您应该总是这样做:

.subscribe(
  x => console.log('x1: ', x),
  err => console.error(err)
);

in nodejs the process is killed when there is an error.
you should always do it like this:

.subscribe(
  x => console.log('x1: ', x),
  err => console.error(err)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文