简体   繁体   English

RxJs:如何进行同步请求?

[英]RxJs: How can I make synchronous requests?

I'm trying to make thousands of http requests at once and my internet connection collapse :)我试图一次发出数千个 http 请求,但我的互联网连接崩溃了 :)

That's why I was looking to implement a function using RxJS Observables that limits simultaneous asynchronous requests to a defined maximum number.这就是为什么我希望使用RxJS Observables实现一个函数,该函数将同时异步请求限制为定义的最大数量。

For example, if the max number would be 3, the requests would work like this:例如,如果最大数量为 3,则请求将如下工作:

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这是一个 JSBin 示例,其中所有请求都使用请求模拟函数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!谢谢!

You can have a look at merge(maxConcurrency) or flatmapwithmaxconcurrent overload.您可以查看merge(maxConcurrency)flatmapwithmaxconcurrent重载。 You could start by reviewing these two SO questions : How to limit the concurrency of flatMap?您可以从查看这两个 SO 问题开始: How to limit the concurrency of flatMap? , Limit number of requests at a time with RxJS and then the documentation links : , 使用 RxJS 限制一次请求的数量,然后是文档链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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