user2963585
user2963585

Reputation: 63

Queuing using RxJS Observables - Angular 5

I'm developing an SPA using Angular 5.

I have an scenario where I need to make multiple calls to a Rest API method, below is an example case.

I have a list of objects (say 100 objects) which will be passed as an parameter to API method. I need to queue the objects by picking the first 10 objects(async API calls), if any of the call responds I need to push the next Object to the queue until all the 100 objects has been processed.

Is this possible to achieve technically. If yes could anyone advise how this can be achieved.

Thanks in advance.

Upvotes: 1

Views: 3463

Answers (1)

martin
martin

Reputation: 96891

You can do this with just mergeMap that takes an optional second parameter that tells how many concurrent subscriptions you want it to keep (btw concatMap is just mergeMap with 1 concurency):

from([obj1, obj2, obj3, ..., obj100])
  .pipe(
    mergeMap(obj => makeHttpCall(obj), 10),
  )
  .subscribe(...);

This will always keep 10 HTTP call active. When one completes it'll pick another one immediately.

Upvotes: 2

Related Questions