Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Angular/RxJS:即使在发生错误的情况下也保持间隔运行_Angular_Typescript_Rxjs - Fatal编程技术网

Angular/RxJS:即使在发生错误的情况下也保持间隔运行

Angular/RxJS:即使在发生错误的情况下也保持间隔运行,angular,typescript,rxjs,Angular,Typescript,Rxjs,我需要每10秒呼叫一次休息服务。REST客户端调用位于angular服务(myservice,函数foo)中 只要与REST服务的连接正常,就可以正常工作。但是如果我停止,那么间隔也停止了 即使REST服务关闭,我需要做什么来保持它运行(并失败?FlatMap处理错误的内部可观察值。在内部可观察对象上,向管道添加一个catchError处理程序,该管道使用空的可观察对象静默处理错误。例如: 间隔(10000)。管道( flatMap(num=>of(num).pipe( mergeMap(num

我需要每10秒呼叫一次休息服务。REST客户端调用位于angular服务(myservice,函数foo)中

只要与REST服务的连接正常,就可以正常工作。但是如果我停止,那么间隔也停止了


即使REST服务关闭,我需要做什么来保持它运行(并失败?

FlatMap
处理错误的内部可观察值。在内部可观察对象上,向管道添加一个
catchError
处理程序,该管道使用
空的
可观察对象静默处理错误。例如:

间隔(10000)。管道(
flatMap(num=>of(num).pipe(
mergeMap(num=>this.myservice.foo()),
catchError((错误)=>{
console.log(“错误”);
返回空();
})
))
).订阅(resp=>{
控制台日志(resp)
});

您可以使用retyWhen错误运算符

interval (10000)
  .pipe(
    startWith(0),
    switchMap (obs => this.myservice.foo()),
    retryWhen(error => {
      return error.flatMap((error: any) => {
          if(error.status  === 503) {
              return Observable.of(true);
          }
          return Observable.throw({error: 'No retry'});
      });
);

这并不是对特定场景的具体回答(我希望在尝试发布建议代码之前先进行stackblitz测试)。但这是一段视频的屏幕截图,该视频讨论了与大沃德钟的错误隔离

注意这两段代码上面的“错误方式”和“正确方式”注释


你可以在这里找到谈话的这一部分:

把错误放在“内部”可观测数据(实际上可能产生错误的数据)上,而不是放在整个数据流上,怎么样?比如:

ngOnInit () {
  interval(10000).pipe(
    startWith(0),
    mergeMap(obs => this.myservice.foo().pipe(
      catchError((error) => {
        console.log(error);
        return empty(); // or return of(error) and do sth about it in the subscribe body
      }),
    ),
  )).subscribe(resp => this.data = resp);
}

你能组织一次闪电战吗?通过这种方式,我们可以在这个特定场景中尝试一些错误隔离技术,以查看在发布之前哪些技术有效。您可以在此处看到一个关于错误隔离的视频:这会继续间隔吗?我想不是吗?“如果发生错误,则上游可观测对象已死亡”。参见22:42的上述链接。
ngOnInit () {
  interval(10000).pipe(
    startWith(0),
    mergeMap(obs => this.myservice.foo().pipe(
      catchError((error) => {
        console.log(error);
        return empty(); // or return of(error) and do sth about it in the subscribe body
      }),
    ),
  )).subscribe(resp => this.data = resp);
}