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

时间:2018-09-06 18:22:01

标签: angular typescript rxjs angular6 rxjs6

我有以下代码尝试并行处理一组端点:

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 个答案:

没有答案