简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM