代码之家  ›  专栏  ›  技术社区  ›  a_zatula

对每个已处理的url进行不必要的调用[重复]

  •  0
  • a_zatula  · 技术社区  · 6 年前

    我在这里创建了角度项目: https://stackblitz.com/edit/angular-5dmqwh

    我有一个不必要的执行问题,在 isLoading 得到 false 对于每个处理过的url。在控制台中执行这种不受欢迎的 url1 看起来像:

     `in map --> url1                       app.component.ts:26` 
     `before request --> url1               app.component.ts:29`
    

    为了 url2 :

     `in map --> url2                       app.component.ts:26` 
     `before request --> url2               app.component.ts:29`
    

    请检查控制台以检查项目输出。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Claudiordgz    6 年前

    似乎您希望请求停止,但是流无法知道这是否是错误。一种方法是检查错误并切换行为,例如,我检查它是否为false,并通过抛出:

    this.counter$.pipe(
      map(counter => {
        if (counter !== 0) {
          return true;
        } else {
          throw(new Error("explosion")); // This will stop the stream, but you should handle it
        }
      }),
    ).subscribe(val => {
       console.log('isLoading ...', val);
    });