``Promise.ly)遭受浏览器施加的并发连接的极限吗?

发布于 2025-02-04 07:09:58 字数 365 浏览 2 评论 0原文

假设API服务器正在使用HTTP/1.1,并且浏览器的限制为每个域的6并发TCP连接。 这是否意味着如果我一次使用Promise.All一次进行7个API调用,那么最后一个api将必须等待第一个api'要求返回网络?

Promise.all([api(), api(), api(), api(), api(), api(), api()]) // 7 api calls

HTTP/2是否也可以通过多路复用来解决此问题。所有这些API调用都将在同一连接上,因此他们不需要等待任何东西?

Assume an api server is using HTTP/1.1 and the browser has a limit of 6 concurrent TCP connections per domain.
Does that mean if I do 7 api calls at once with Promise.all then the last api will have to wait for the first api's request to come back over the network?

Promise.all([api(), api(), api(), api(), api(), api(), api()]) // 7 api calls

Also does HTTP/2 solve this problem with multiplexing where all the these API calls will be on the same connection so they don't need to wait for anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2025-02-11 07:09:58

这是否意味着如果我立即使用Promise进行7个API呼叫。所有的API都必须等待第一个API的请求才能返回网络?

是的,确实如此。

您的Promise.All([API(),API(),...])语句将立即运行所有api()函数调用。这将导致前6个请求被发送,并且第7请求将排队,直到前6个完成之一,在这种情况下,浏览器将发送第七。

这种行为实际上与Promise.all()没有任何关系。如果您只是这样做的话,也会发生相同的行为:

const promises = [api(), api(), api(), api(), api(), api(), api()];

即使仅使用该声明,第七http请求才能发送到主机,直到前6个完成之一。这完全是因为浏览器的连接限制了同一主机。

仅供参考,Promise.all()仍然可以使用,并且所有API请求仍然可以正常工作,只需等待所有请求即可完成的更长的时间,而不是您没有达到连接极限。

Does that mean if I do 7 api calls at once with Promise.all then the last api will have to wait for the first api's request to come back over the network?

Yes, it does.

Your Promise.all([api(), api(), ...]) statement will immediately run all api() function calls. That will cause the first 6 requests to get sent and the 7th will be queued until one of the first 6 finishes in which case the browser will the send the 7th.

This behavior doesn't actually have anything to do with Promise.all(). The same behavior would occur if you just did this:

const promises = [api(), api(), api(), api(), api(), api(), api()];

Even with just that statement, the 7th http request wouldn't get sent to the host until one of the first 6 had completed. It's entirely because of the browser's connection limits to the same host.

FYI, the Promise.all() will still work and all the api requests will still work, it will just have to wait a bit longer for all requests to complete than if you weren't hitting the connection limits.

欲拥i 2025-02-11 07:09:58

是的,但这不是Promise.all的限制。通常,

anotherFn([fn(), fn(), fn()])

操作一样,等同于

const r1 = fn();
const r2 = fn();
const r3 = fn();
anotherFn([r1, r2, r3]);

像在结果传递之前对参数列表中的所有 在参数列表中进行所有表达式。如果评估所有内容的过程都会引起一系列API调用,则其他功能 - Promise.allenternfn不可能做任何事情,因为这些函数发生在功能运行之前发生。

然后,最后一个API必须等待第一个API的请求才能返回网络?

如果我们可以假设限制如您所说的 - 浏览器仅限于与API的6个连接,那么是的。

是的,HTTP/2在这种情况下有很大帮助,因为它们每个域只需要一个连接,并且可以在该连接上发送多个请求,而无需遇到浏览器连接限制。

Yes, but this isn't a limitation particular to Promise.all. In general, an expression like

anotherFn([fn(), fn(), fn()])

is equivalent to doing

const r1 = fn();
const r2 = fn();
const r3 = fn();
anotherFn([r1, r2, r3]);

Everything inside the argument list gets evaluated before the result is passed. If the process of evaluating everything makes a bunch of API calls, the other function - Promise.all or anotherFn cannot possibly do anything about that, because those occur before the function runs.

then the last api will have to wait for the first api's request to come back over the network?

If we can assume that the restrictions are as you've said - the browser is limited to 6 connections to the API at once - then yes.

Yes, HTTP/2 is of great help in these sorts of situations, because they need only one connection per domain, and can send multiple requests over that connection, without running into the browser connection limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文