简体   繁体   English

基于第一次 POST 调用的响应的角度 http POST 调用重试

[英]angular http POST call retry based on the response of first POST call

I have a scenario where I need to retry the post call based on the response flag from the first attempt of the same post call.我有一个场景,我需要根据第一次尝试同一个 post 调用的响应标志重试 post 调用。

I have following service call我有以下服务电话

this.CommonService.postRequest(this.RequestData)
.subscribe((data:Response) => 
{ if(data){ 
//based on the flag in the response data I need to make same call again until the flag is false. 
} });

I will get a flag in the response when I make the above call.当我拨打上述电话时​​,我会在响应中得到一个标志。 If I get a flag true then I need to retry same call.如果我得到一个 true 标志,那么我需要重试相同的调用。 I can retry the same call for maximum 3 times.我最多可以重试同一个调用 3 次。

any idea how to handle it?知道如何处理吗?

use the switchMap operator to wait for the response of the first request to arrive before firing the second request.使用switchMap操作符在触发第二个请求之前等待第一个请求的响应到达。

return this.http.get('url/1')
  .switchMap(res1 => {
    // use res1 response
    this.http.get('url/2')
  })
  .subscribe(res2 => {

  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM