gpt4 book ai didi

angular - Angular 6 + RxJs-concatMap的错误处理

转载 作者:行者123 更新时间:2023-12-03 07:52:50 24 4
gpt4 key购买 nike

我仍在学习RxJ,并且尝试使用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)
    );

    https://stackblitz.com/edit/rxjs-6dign7

    关于angular - Angular 6 + RxJs-concatMap的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54952297/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com