【问题标题】:RxJs: get array of observablesRxJs:获取可观察的数组
【发布时间】:2021-11-01 22:27:33
【问题描述】:

我有以下伪代码

zip(
        ...[1, 2, 3].map((id) =>
          this.http.function1(id).pipe(
            mergeMap((obj1) => this.http.function2(obj1.id)),
            mergeMap((obj2) => this.http.function3(obj2.id)),
          ),
        ),
      ).subscribe((result) => {
        console.log('This should be an array of all the results of the requests to this.http.function3');
      });

我想把所有请求的结果放在一起。我该怎么做?

【问题讨论】:

    标签: angular rxjs rxjs-observables


    【解决方案1】:

    而不是zip(),我将使用from() 发出数组的每个值,然后在管道的末尾,您可以应用toArray() 在所有HTTP 请求完成后将所有发出的值组合到一个数组中.

    from([1, 2, 3]).pipe(
      mergeMap(id =>
        this.http.function1(id).pipe(
          mergeMap(obj1 => this.http.function2(obj1.id)),
          mergeMap(obj2 => this.http.function3(obj2.id))
        )
      ),
      toArray()
    ).subscribe(result=>{
      console.log('Result should now be an array from function3', result);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 2020-05-02
      • 2016-06-18
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多