【问题标题】:Rxjs concat map condition check before it go to second callRxjs concat map 条件检查,然后再进行第二次调用
【发布时间】:2020-07-27 11:42:07
【问题描述】:

我想在继续之前检查第一次服务调用结果。如果第一次通话失败,我有一些事情要处理

    return this.http.get(this.partySiteAddressUrlOrganization + "?" + new Date().toString(), { params: newparams }).pipe(
  concatMap((result1: any) => {
    if (result1.success)
      return this.http.post(this.postRequestListURL, parm)
    else {
    }

  }));

【问题讨论】:

    标签: angular rxjs rxjs-pipeable-operators concatmap


    【解决方案1】:

    不完全理解您的问题,根据我的假设,这是我的想法

    如果您的 API 抛出任何错误/异常,您希望处理。

    您可以简单地使用 rxjs/operators 中的 catchError 运算符并编写类似这样的内容。

    return this.http.get(this.partySiteAddressUrlOrganization + "?" + new Date().toString(), { params: newparams })
    .pipe(
      concatMap((result1: any) => {
          return this.http.post(this.postRequestListURL, parm)
        }),
    catchError((error)=>{
     // handle the error
     // if you want to throw it the throw other wise send the EMPTY its up to you
     // using return EMPTY;
    }));
    

    如果第一个不是您的方案,并且您说如果出现问题,您的 API 将返回一个标志,那么您可以像这样处理

    return this.http.get(this.partySiteAddressUrlOrganization + "?" + new Date().toString(), { params: newparams })
        .pipe(
          concatMap((result1: any) => {
              if(result1.success) {
                 return this.http.post(this.postRequestListURL, parm)
             } else {
              // handle your stuff here and the return 
               return of({}); // {} can be your object 
             }
            }),
        catchError((error)=>{
         // handle the error
         // if you want to throw it the throw other wise send the EMPTY its up to you
        }));
    

    【讨论】:

    • 感谢您的回答,我的是第二个场景。它工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多