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而接受了其他答案-非常感谢抱歉现在接受了你的答案-向你和其他人致敬,其他人对此很满意。。。谢谢