【问题标题】:How to implement in angular multiple sequential Http requests in a loop (each new request depends of the previous results)如何在循环中以角度实现多个顺序 Http 请求(每个新请求取决于先前的结果)
【发布时间】:2020-01-16 10:48:21
【问题描述】:


我有一个网格,其中包含要加载的大量数据,因此加载时间变得难以管理。我找到的解决方案是执行连续数量的 http 请求,每个请求检索 100 行的批次,直到完成网格上的所有数据。我知道如何使用 concatMap 实现 2 个连续的 http 请求,它运行良好,但我希望有一个 while 循环来验证每个响应,如果当前行数

使用 concatMap 执行 2 个 http 请求的转发代码:

private LoadGridStringResourcesInProject(projectId: number) {
let allData: StringResource[] = [];
const batchSize = 100;

this.stringResourcesService.getStringResourcesInProject(projectId, 0, batchSize)
    .pipe(
      concatMap(firstData => {
        const stringResourcesInProject = firstData as StringResource[];

        // Loads first 100 rows on the Grid
        this.gridApi.updateRowData({ add: stringResourcesInProject });
        this.agGridService.refreshSizeColumns(this.agGrid);

        // Fetch data returned by partial Http requests
        allData = stringResourcesInProject;

        if (allData && allData.length == batchSize) {

          // Do new Http request to fetch the remaining data
          return this.stringResourcesService
            .getStringResourcesInProject(projectId, batchSize, 0);
        }

        return [];
      })
    ).subscribe(data => {
        const stringResourcesInProject = data as StringResource[];

        // Loads the remaining rows in the Grid
        this.gridApi.updateRowData({ add: stringResourcesInProject });

        // Fetch data returned by partial Http requests
        allData = allData.concat(stringResourcesInProject);
      },
      error => of(null),
      () => {
        this.agGridService.refreshSizeColumns(this.agGrid);
      });

}

【问题讨论】:

  • 你要找的接线员是expand :)
  • 非常感谢@Maxim1992。像魅力一样工作:)

标签: angular http rxjs concatmap


【解决方案1】:

就像@Maxim1992 所说,解决方案是使用 Expand RxJS 运算符来递归调用! 非常感谢@Maxim1992!

更多信息在这里:Example

这是你也可以使用的代码(希望它可以帮助将来的人):

private LoadGridStringResourcesInProject(projectId: number) {
const batchSize = 1000;
let iteraction = 0;


this.stringResourcesService.getStringResourcesInProject(projectId, false, false, false, 0, batchSize)
  .pipe(
    expand(partialData => {
      if (partialData) {
        let partialStringResourcesInProject = partialData as StringResource[];

        if (partialStringResourcesInProject.length > batchSize) {

          // Loads the remaining rows in the Grid
          this.gridApi.updateRowData({ add: partialStringResourcesInProject });

          iteraction += 1;

          return this.stringResourcesService.getStringResourcesInProject(projectId, false, false, false,
                       batchSize * (iteraction - 1), batchSize);
        }

        return EMPTY;
      }
    })
  ).subscribe(data => {

        //... 
  },
  error => of(null),
  () => {
    this.agGridService.refreshSizeColumns(this.agGrid);
  });

【讨论】:

  • 很高兴您在听说过 expand 后找到了解决方案 :) 干得好
猜你喜欢
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
相关资源
最近更新 更多