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

控制并行度,确保操作完成

  •  1
  • MHOOS  · 技术社区  · 7 年前

    我有以下代码试图并行处理端点数组:

    callApiEndpoint(endpoint: string): Observable<any> {
      return this.httpClient
        .get<any>(endpoint)
        .pipe(
          tap(
            data =>
              console.log(`calling ${endpoint} returned ${JSON.stringify(data)}`),
            catchError(ApiCallerComponent.handleError)
          )
        );
    }
    
    getEndpointsToProcess(): string[] {
      const result = [];
    
      this.idsToProcess.forEach(element => {
        result.push(this.getEndpoint(element));
      });
    
      return result;
    }
    
    processRequests() {
      const endpoints = this.getEndpointsToProcess();
    
      forkJoin(endpoints.map(uri => <Observable<any>>this.callApiEndpoint(uri)))
        .pipe(concatAll())
        .subscribe(val => console.log(val));
    }
    

    如何控制向API服务器发出的并行请求的程度?此外,无论结果如何,我怎么能说所有请求都已完成?

    0 回复  |  直到 7 年前
    推荐文章