我仍在学习 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
。但只要您不想改变错误对象,我宁愿在管道末尾有一个 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)
);
关于Angular 6 + RxJs - concatMap 的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54952297/