Angular 角度6+;RxJs-concatMap的错误处理

Angular 角度6+;RxJs-concatMap的错误处理,angular,error-handling,rxjs6,concatmap,Angular,Error Handling,Rxjs6,Concatmap,我仍在学习RxJs,并尝试使用concatMap()不使用嵌套订阅。我希望第一个调用运行,然后在运行第二个请求之前延迟一两秒钟(在第二个请求之前创建数据库记录)。我还想专门为每个请求添加错误处理,以便能够单独捕获它们的错误 到目前为止,我有一些东西运行请求1,延迟,然后运行请求2 return this.request_1(postData).pipe( concatMap(res => of(res).pipe( delay( 2000 ) )), concatM

我仍在学习RxJs,并尝试使用concatMap()不使用嵌套订阅。我希望第一个调用运行,然后在运行第二个请求之前延迟一两秒钟(在第二个请求之前创建数据库记录)。我还想专门为每个请求添加错误处理,以便能够单独捕获它们的错误

到目前为止,我有一些东西运行请求1,延迟,然后运行请求2

return this.request_1(postData).pipe(
      concatMap(res => of(res).pipe( delay( 2000 ) )),
      concatMap(res => this.request_2(parseInt(data.id), parseInt(model['_id'])) )
    );
我想知道的是-

  • 我可以在每个请求上使用类似catchError()的东西吗
  • 如果我希望在第二个请求运行之前完成请求1,这是否正确

  • 谢谢

    您可以在每个请求中添加一个
    catchError
    。但只要你不想改变error对象,我宁愿在管道末端有一个
    catchError
    。这只会使订阅者产生每一个错误

    然后可以在订阅中完成错误处理本身

    const source = of('World').pipe(
      concatMap(res => of(res).pipe(
        delay(2000),
        map(res => res += ' + concat 1')
      )),
      concatMap(res => of(res).pipe(
        map(res => res.h += ' + concat 2')
      )),
      catchError(err => throwError(err))
    );
    
    source.subscribe(
      x => console.log(x),
      error => console.log('error', error)
    );