【问题标题】:how to continue http until result is found如何继续http直到找到结果
【发布时间】:2021-08-17 20:02:48
【问题描述】:

我有一个 http 调用,当开始调用前几秒钟是错误 404,直到结果进入数据库:

this.http.get(`http://localhost:44301/consentinitiation/${this.qid}`).subscribe(s=>{
 console.log(s);
 },error=>(error));

我的想法是把它放在里面

        shouldCall=true;
        while(shouldCall){
         this.http.get(`http://localhost:44301/consentinitiation/${this.qid}`).subscribe(s=>{
         console.log(s);
         shouldCall=false;
         },error=>(error));}

问题是浏览器使用这种方法会崩溃,知道吗?

【问题讨论】:

标签: angular typescript rxjs


【解决方案1】:

最简单的方法是使用retry 运算符,它将重试流直到成功

this.http.get(`http://localhost:44301/consentinitiation/${this.qid}`)
.pipe(retry())
subscribe(s=>{
 console.log(s);
 },error=>(error));

您还可以添加一个参数来指定最大重试次数,例如retry(5)

【讨论】:

  • 我怎么能说每 5 秒重试一次?
  • 您可以在retry之前添加delay(5000)运算符。
猜你喜欢
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
相关资源
最近更新 更多