Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RxJS在Angular中批处理HTTP请求_Angular_Rxjs - Fatal编程技术网

RxJS在Angular中批处理HTTP请求

RxJS在Angular中批处理HTTP请求,angular,rxjs,Angular,Rxjs,我有这样一个数组: const mynums = [[1,2], [3,4]] // this array may contain any number of integer arrays 使用此命令,我希望发出以下HTTP请求: http://myserver.com/resource/1 // 2 requests made simultaneously http://myserver.com/resource/2 http://myserver.com/resource/3 // 2

我有这样一个数组:

const mynums = [[1,2], [3,4]] // this array may contain any number of integer arrays
使用此命令,我希望发出以下HTTP请求:

http://myserver.com/resource/1 // 2 requests made simultaneously
http://myserver.com/resource/2

http://myserver.com/resource/3 // 2 requests made simultaneously but only after first 2 complete
http://myserver.com/resource/4
我知道我可以像这样使用forkJoin:

forkJoin([
  this.http.get('http://myserver.com/resource/1')
  this.http.get('http://myserver.com/resource/2')
]).subscribe(
  (responses) => {
    console.log(responses[0])
    console.log(responses[1])
  }
)
但是,我如何处理HTTP请求以适应长度可变的
mynums
数组,并确保第一批请求在发出第二批请求之前完成?第三批、第四批还是第N批

许多thx尝试以下方法:

source=from([[1,2],[3,4]]);
来源
.烟斗(
映射(idies=>idies.map(id=>)https://jsonplaceholder.typicode.com/todos/“+id”),
concatMap(url=>forkJoin(…url.map(url=>this.http.get(url)))
)
.subscribe(x=>console.log(x))

整个代码

您可以使用concatMap&forkJoin在第一批完成后发出其他批处理请求

from(mynums).pipe( 
     concatMap (x=> {
       return forkJoin(x.map(t=>{
         return this.http.get(`http://myserver.com/resource/${t}`)
       }))
     })
).subscribe(console.log)

回答得很好,很多thx-因为他只回答了b4 u而接受了其他答案-非常感谢抱歉现在接受了你的答案-向你和其他人致敬,其他人对此很满意。。。谢谢