简体   繁体   English

在angular 6中使用takeWhile在使用http轮询api时无响应

[英]No response while using takeWhile in angular 6 for polling api using http

I have a client API which i want to poll till it returns success result, this API returns a response with following JSON. 我有一个要轮询的客户端API,直到返回成功结果为止,此API返回带有以下JSON的响应。

{
"count": 0,
"meta": {
    "status": "PENDING",
    "total": 3185,
    "completion_percentage": 0,
    "total_available": 0,
    "hotels_filtered": 0,
    "offers": 0
},
data:[]
}

and a positive result should contain the status ='COMPLETED' and data filed will include the result in data, I tried using takeWhile from 'rxjs' 并且肯定的结果应包含状态='COMPLETED',并且归档的数据将结果包含在数据中,我尝试使用来自'rxjs'的takeWhile

component.ts component.ts

var headers = { headers: new HttpHeaders({ 'x-user-agent':'M;B2B' }) };

var url='https://api.xyz.com/hotels/v1/search/entity/27548283?apikey=1234567'

this.http.get(url,headers)
.pipe(
  takeWhile((data)=>JSON.parse(JSON.stringify(data)).meta.status==='COMPLETED') 
)
.subscribe((result)=>{
  console.log(result);

},(err)=>{
  console.log(err);

})

But unfortunately I am not getting anything in to subscribe log method, is this a right method? 但是不幸的是我没有任何订阅日志方法,这是正确的方法吗?

My angular version is 6. 我的角度版本是6。

I think you are looking for retryWhen operator: 我认为您正在寻找retryWhen运算符:

let httpGet$ = this.http.get(url, headers);
let retryFilter = data => JSON.parse(JSON.stringify(data)).meta.status !== 'COMPLETED';

httpGet$.pipe(
  map(data => {
    if(retryFilter(data)) {
      throw 'retry';
    }
    return val;
  }),
  retryWhen(_ =>
    // retry after 3 sek when retryFilter (above) failed
    _.pipe(delayWhen(_ => timer(3000))),
  )
).subscribe(result => console.log(result));

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

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