2

I'm trying to make thousands of http requests at once and my internet connection collapse :)

That's why I was looking to implement a function using RxJS Observables that limits simultaneous asynchronous requests to a defined maximum number.

For example, if the max number would be 3, the requests would work like this:

r-----R--->
r---R----->
r-------R->
----r---R->
------r--->
--------r->
--------r->

Here is a JSBin example where all the request are sent at once with a request mocked function http://jsbin.com/vemewu/4/edit?js,console

function makeTestRequest() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000);
  });
}

let requests = _.range(0, 100).map(() => makeTestRequest);

var requests$ = Rx.Observable.fromArray(requests);

requests$.subscribe(
  () => console.log('request done'),
  () => console.log('error'),
  () => console.log('completed')
);

Thanks!

Community
  • 1
  • 1
Roman Oxman
  • 377
  • 6
  • 18

1 Answers1

1

You can have a look at merge(maxConcurrency) or flatmapwithmaxconcurrentoverload. You could start by reviewing these two SO questions : How to limit the concurrency of flatMap?, Limit number of requests at a time with RxJS and then the documentation links :

Community
  • 1
  • 1
user3743222
  • 18,345
  • 5
  • 69
  • 75