RxJS如何忽略catch的错误并继续前进 [英] RxJS how to ignore an error with catch and keep going

查看:271
本文介绍了RxJS如何忽略catch的错误并继续前进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我想知道如何防止抛出错误时删除主要的(上游)Observable。

Hi I have the following code and I would like to know how to prevent the main (upstream) Observable from getting deleted when an error is thrown.

如何我更改以下代码,以便所有数字都期望显示 4?

How can I change the following code so that all numbers expect '4' get displayed?

我正在寻找一种通用模式解决方案,该解决方案在其他情况下也可以使用不同的运算符。这是我能想到的最简单的情况。

I am looking for a general pattern solution that would work in other cases with different operators. This is the simplest case I could come up with.

const Rx = require('rxjs/Rx');

function checkValue(n) {
  if(n === 4) {
    throw new Error("Bad value");
  }
  return true;
}
const source = Rx.Observable.interval(100).take(10);

source.filter(x => checkValue(x))
  .catch(err => Rx.Observable.empty())
  .subscribe(v => console.log(v));


推荐答案

您将希望保持可观察源的运行,但是如果您让错误发生在主事件流上,它将折叠整个可观察的对象,并且您将不再接收任何物品。

You will want to keep the source observable running, but if you let the error happen on the main event stream it will collapse the entire observable and you will no longer receive items.

该解决方案涉及创建一个分离的流,您可以在其中过滤和捕获而不会使上游管道崩溃。

The solution involves creating a separated stream where you can filter and catch without letting the upstream pipe collapse.

const Rx = require('rxjs/Rx');
function checkValue(n) {
  if(n === 4) {
    throw new Error("Bad value");
  }
  return true;
}
const source = Rx.Observable.interval(100).take(10);

source
  // pass the item into the projection function of the switchMap operator
  .switchMap(x => {
     // we create a new stream of just one item
     // this stream is created for every item emitted by the source observable
     return Observable.of(x)
       // now we run the filter
       .filter(checkValue)
       // we catch the error here within the projection function
       // on error this upstream pipe will collapse, but that is ok because it starts within this function and will not effect the source
       // the downstream operators will never see the error so they will also not be effect
       .catch(err => Rx.Observable.empty());
     })
     .subscribe(v => console.log(v));

您还可以使用传递给catch选择器的第二个参数重新启动可观察的源,但这将

You could also use the second argument passed into the catch selector to restart the observable source, but this will start it as though it hasn't run before.

const Rx = require('rxjs/Rx');

function checkValue(n) {
  if(n === 4) {
    throw new Error("Bad value");
  }
  return true;
}
const source = Rx.Observable.interval(100).take(10);

source.filter(x => checkValue(x))
  .catch((err, source) => source)
  .subscribe(v => console.log(v));

但这无法达到预期的效果。您将看到一个流重复发出1..3,直到时间结束......或您关闭脚本。以先到者为准。 (这是 .retry()所做的必不可少的操作)

But this does not achieve the desired effect. You will see a stream that emits 1..3 repeatedly until the end of time... or you shutdown the script. Which ever comes first. (this is essential what .retry() does)

这篇关于RxJS如何忽略catch的错误并继续前进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆