0

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

    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 {
    }

  }));
4

1 回答 1

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
    }));
于 2020-07-27T12:19:10.287 回答