Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 limit parallel http calls

Tags:

angular

rxjs

I've got an Angular 2 app, which fetches a varying number of ids from a server, and then, for each id makes another REST call in a forkJoin.

However, the amount of ids can go up to a few hundreds, which might be problematic when suddenly making several hundred REST calls in parallel.

Is there a way of limiting the number of parallel calls when using RxJs and the forkJoin operator?

like image 293
user1839433 Avatar asked Aug 12 '26 19:08

user1839433


1 Answers

Onw way would be to use bufferCount:

Rx.Observable.from([1,2,3,4,5,6,7,8])
  .bufferCount(3)
  .concatMap(items => {
    console.log('ConcatMap', items);
    let tasks = items.map(item => Rx.Observable.timer(Math.random() * 1000).do(() => console.log(item, 'ready')));
    return Rx.Observable.forkJoin(...tasks);
  })
  .subscribe()
<script src="https://npmcdn.com/@reactivex/[email protected]/dist/global/Rx.js"></script>
like image 64
Sergey Sokolov Avatar answered Aug 14 '26 09:08

Sergey Sokolov