Limit number of requests at a time with RxJS
It's 2018, rxjs 5 is here and this is how I solved it
urls$
.mergeMap((url) => request({ url }), null, 10)
.subscribe()
mergeMap
(aka flatMap
) already takes the "max concurrency" as its 3rd parameter (see the docs)
Btw. I am using universal-rxjs-ajax (the request
) for node compatibility, but it should work the same with Observable.ajax
RxJS v6 update
pipe through mergeMap with your parallel limit as 2nd parameter
const MAX_PARALLEL_QUERIES = 3;
let allResults = [];
let observables = [] // fill with observables
from(observables)
.pipe(mergeMap(observable => observable, MAX_PARALLEL_QUERIES))
.subscribe(
partialResults => {
allResults = allResults.concat(partialResults);
},
err => {
// handle error
},
() => {
// get here when all obserable has returned
allResults.forEach(result=> {
// do what you want
});
}
);