Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RxJS如何忽略捕获错误并继续_Javascript_Exception_Error Handling_Rxjs5 - Fatal编程技术网

Javascript RxJS如何忽略捕获错误并继续

Javascript RxJS如何忽略捕获错误并继续,javascript,exception,error-handling,rxjs5,Javascript,Exception,Error Handling,Rxjs5,您好,我有以下代码,我想知道如何防止在抛出错误时删除主(上游)可观察对象 如何更改以下代码,以便显示除“4”之外的所有数字 我正在寻找一个通用的模式解决方案,可以在其他情况下与不同的运营商合作。这是我能想到的最简单的例子 const Rx = require('rxjs/Rx'); function checkValue(n) { if(n === 4) { throw new Error("Bad value"); } return true; } const sourc

您好,我有以下代码,我想知道如何防止在抛出错误时删除主(上游)可观察对象

如何更改以下代码,以便显示除“4”之外的所有数字

我正在寻找一个通用的模式解决方案,可以在其他情况下与不同的运营商合作。这是我能想到的最简单的例子

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));

您需要使用flatMap操作符进行过滤。在本例中的flatMap中,我使用Observable.if()进行过滤,因为它保证我始终返回Observable。我相信你可以用其他方法来实现,但这对我来说是一个干净的实现

const source = Rx.Observable.interval(100).take(10).flatMap((x)=>
    Rx.Observable.if(() => x !== 4, 
    Rx.Observable.of(x),
    Rx.Observable.throw("Bad value"))
    .catch((err) => {
        return Rx.Observable.empty()
    })
);

source.subscribe(v => console.log(v));

您将希望保持源可观察的运行,但是如果让错误发生在主事件流上,它将折叠整个可观察的,您将不再接收项目

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

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选择器的第二个参数来重新启动可观察源,但这将启动它,就好像它以前没有运行过一样

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()
的基本功能)

谢谢!我几乎想出了相同的解决方案,但是我最终使用flatMap只是因为它读起来更好,但做的和switchMap一样。关于将参数传递给catch的第二个解决方案对我来说是新的,谢谢分享。flatMap是mergeMap的别名。我建议切换到mergeMap或switchMap,因为它在v5中更为惯用。flatMap是一种传统的保留。
mergeMap
而不是
switchMap
起作用
switchMap
似乎在内部
observable.empty()
发出有效意味着外部无法按预期继续运行的完成事件时,将完成传播到父级observable。我使用RxJs 6。内部流的较新语法是:
of(x).pipe(filter(checkValue),onErrorResumeNext)
Observable.if()
似乎在v5中被删除了。我相信它刚刚被重命名为
iif()