【问题标题】:rxjs merge http responserxjs 合并 http 响应
【发布时间】:2020-09-04 03:56:50
【问题描述】:

多个http请求是并发的,可以控制并发请求的数量。所有请求完成后,可以合并数据(数据可以有序也可以无序)

from([link, link, link]).pipe(
   merge(l => request.get(l)),
   // something...
)

return [linkdata, linkdata, linkdata]

【问题讨论】:

    标签: javascript http rxjs


    【解决方案1】:

    考虑如下实现它:

    from([link1, link2, link3]).pipe(
      mergeMap(request.get, CONCURRENT_NUMBER), // <= limit the number of active inner subscriptions at a time
      toArray() // <= transform stream into array prior to completion 
    )
    .subscribe(([result1, result2, result3]) => { })
    

    【讨论】:

      【解决方案2】:

      我想你正在搜索mergeAll

      from([link1, link2, link3].map(link => request.get(link))).pipe(
        mergeAll(CONCURRENT_NUMBER),
        // something like reduce, toArray... to combine the results
      ).subscribe(console.log);
      

      【讨论】:

        【解决方案3】:

        看来你需要使用forkJoin

        const links = [link1, link2, link3]; // assump `links` is an array of string
        
        forkJoin(
          from(links).pipe(
            mergeMap((link: string) => {
              return http.get(link); // returns an observable
            }),
            toArray()
          )
        ).pipe(
           map(array => array[0]) // data stream is in [[data]] format, you only need head
        ); // [link1data, link2data, link3data]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-04-21
          • 2023-03-17
          • 1970-01-01
          • 2017-09-04
          • 2018-12-27
          • 2022-01-25
          • 1970-01-01
          相关资源
          最近更新 更多