【问题标题】:Can I change the query params value on Rxjs Retry?我可以更改 Rxjs Retry 上的查询参数值吗?
【发布时间】:2018-06-27 10:42:24
【问题描述】:

我有这项服务,最初返回 null(初始化后台作业)[ this.q = true]。但是稍后如果我将其更改为 [ this.q = false] 并且如果作业成功,它将返回值。

所以,首先我需要致电[ this.q = true] 并重试[ this.q = false]。 但是下面的代码不起作用。它总是让https://api.example.com?q=true

return this.http.get('https://api.example.com?q=' + this.q)
  .map((res: any) => {    
    if (res === null) {          
      throw new Error("not enough tiles !");
    }                
    return res;
  }).catch((e) => {    
    this.q = false;
    return error;
  })
  .retryWhen(e => e.delay(2000))
  .retry(3);

【问题讨论】:

  • 你在 "catch" 中有 this.q=false,而不是在 if(res===null){this.q=true;...} 中。响应 null 可以是有效的
  • response null 是有效的,这就是为什么我在 res === null 将控件发送到 catch 时抛出错误的原因。我放了控制台,它就被执行了。
  • 第一个请求 https://api.example.com?q=true 它将返回 null。在那之后,我必须用https://api.example.com?q=false重试。
  • 我认为 throw new Error("..") 不会让程序去“catch”,但我不确定

标签: angular rxjs rxjs5


【解决方案1】:

Retry 重新订阅源 Observable,因此,如果您想更改 retry 上的 get 请求的参数,您应该将参数值的计算作为源 observable 的一部分。例如

return Observable.of('https://api.example.com?q=')
  .concatMap((baseUrl) => this.http.get(baseUrl + this.q))
  .map((res: any) => {    
    if (res === null) {          
      throw new Error("not enough tiles !");
    }                
    return res;
  }).catch((e) => {    
    this.q = false;
    return error;
  })
  .retryWhen(e => e.delay(2000))
  .retry(3);

【讨论】:

  • 或者甚至只是Observable.defer(() => this.http.get('https://api.example.com?q=${this.q}')),map(...).catch(...).retryWhen(...).retry(3) 而不是of().concatMap
【解决方案2】:

支持@zafeiris.m,为我提供了这个解决方案。

不完全是对当前帖子的回答: 我需要调用一个返回可观察票证列表的服务,当收到 http 应答时,再次调用增加页面偏移量的服务,重复直到不存在下一页链接。

searchTickets() {
    const onSearchResult = (listTicketsResponse: ListTicketsResponse) => {
      this.searchOffset += this.searchPageSize;
      this.allSearchedTickets.push(...listTicketsResponse.result.elements);
    }

    of (1) // Create an observable 
    .pipe( // pipe it just to concatened it with http service call 
      concatMap(() => { // concatened it with the http search tickets service, to ensure params are updated when repeatWhen be call
        return this.ticketsService.searchTickets(searchTicketsRequest, this.searchOffset, this.searchPageSize); // return observable of request to API 
      }), // repeat when previous observable complete 
      repeatWhen((obs: Observable < ListTicketsResponse > ) => {
        return obs.pipe(
          delay(100)); // wait a little  
      }),
      retryWhen((obs: Observable < ListTicketsResponse > ) => {
        console.error("ERROR WITH searchTickets WAIT 10 secondes ")
        return obs.pipe(
          delay(10000)
        );
      }), // take while API response tell us next link exist
      takeWhile((listTicketsResponse: ListTicketsResponse) => {
        onSearchResult(listTicketsResponse); // add to list, update offset
        return (listTicketsResponse.result.links.next ? .href ? .length ? ? 0) > 0; // true if next url is not empty
      }),
    ).subscribe();
}

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2023-02-25
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    相关资源
    最近更新 更多