发送请求后以及等待/下载时间之前流产JS fetch()

发布于 2025-01-18 06:40:42 字数 642 浏览 2 评论 0原文

我的问题:

我当前正在尝试将几个fetch()请求发送到URL,而不必等待服务器的响应。

确切的用例是构建一个小的镀铬扩展名,该扩展增加了外部服务器上的计数器(我没有控制),从而增加了请求。

但是,为了用UX,我想尽快做到这一点。

我尝试的是:

我的第一次尝试是在100毫秒之后取消提取。

这可以通过良好的连接和快速的计算机使用,但是如果我没有两个,则fetch()中止太早了:请求永远不会发送。

这是我到目前为止的代码:

    let controller = new AbortController();
    let timer = setTimeout(() => controller.abort(), 100);

    await fetch(url, {signal: controller.signal}).catch(err => {
    if (err.name === 'AbortError') {
      }
    });
    clearTimeout(timer);

问题:

有没有办法知道何时提取已通过了该部分的“发送请求”部分,以便我可以在那里中止它?

My problem:

I am currently trying to send several fetch() requests to a url without having to wait for the server's response.

The exact use case is to build a small chrome extension that increase a counter on an external server (which I do not have control on) that increments on GET requests.

But, for the sake of UX, I would want to do this as quickly as possible.

What I tried :

My first try was to cancel the fetch after 100 millisec.

This works OK with a good connection and quick computer but if I do not have both, the fetch() is aborted too soon : the request is never sent.

Here is the code I have so far :

    let controller = new AbortController();
    let timer = setTimeout(() => controller.abort(), 100);

    await fetch(url, {signal: controller.signal}).catch(err => {
    if (err.name === 'AbortError') {
      }
    });
    clearTimeout(timer);

Question:

Is there a way to know when a fetch has passed the "send request" part of the fetch so I can abort it right there ?

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

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

发布评论

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

评论(2

谎言 2025-01-25 06:40:42

我正在尝试将多个请求发送到URL,而不必等待服务器的响应。

那就是 sendbeAcon sendbeacon 本来要做的,无需参与fetch

如果那不符合您的要求,您仍然可以使用fetch根本不等待响应 - 没有任何迫使您使用等待

有没有办法知道获取何时通过了获取的“发送请求”部分,以便我可以在那里中止它?

不。无论如何,流产的请求不是正确的解决方案。请注意,即使仅在发送HTTP请求后仅关闭连接时,服务器仍可能会注意到这一点,并且不会在没有可写的目的地进行响应目的地处理请求。

I am trying to send several requests to a url without having to wait for the server's response.

That's what sendBeacon is meant to do, no need to involve fetch.

If that doesn't meet your requirements, you can still use fetch and simply not wait for the response - nothing forces you to use await.

Is there a way to know when a fetch has passed the "send request" part of the fetch so I can abort it right there?

No. And aborting the request is not the right solution anyway. Notice that even when closing the connection only after the HTTP request is sent, the server might still notice that and will not process the request without a writable destination for the response.

悲念泪 2025-01-25 06:40:42

如果您想发送请求但不需要回复正文,则应使用 HEAD 方法发送,这指示服务器仅回复调用的标头而不是正文,因此您可以检查状态或标头包中的内容类型等

尽快发送 10 个请求,然后等待响应

const pending=[];
for(let i=0;i<10:i++)
{
    pending.push(fetch(url, {method:"HEAD"});
}
for(const req of pending){
    const res = await req;
    //check for a 200 code or any other bodyless data
}

,或者如果您真的不关心响应,则无需等待承诺完成,

for(let i=0;i<10:i++)
{
    fetch(url, {method:"HEAD"});
}

中止是在您想要终止时使用的呼叫即使它 @Bergi尚未提及

,如果您只想 ping url,那么您可以使用

navigator.sendBeacon(url) 

,但这将发送一个发布请求,让您对如何处理该请求的控制更少

if you want to send a request but don't need the body of the reply you should send using HEAD method, this instructs the server to just reply with the headers of the call and not the body, so you can check for a status or content type etc that is in the header package

send 10 requests as fast as possible then await the reponses

const pending=[];
for(let i=0;i<10:i++)
{
    pending.push(fetch(url, {method:"HEAD"});
}
for(const req of pending){
    const res = await req;
    //check for a 200 code or any other bodyless data
}

or if you really don't care about the response as all there is no need to await the promise completion

for(let i=0;i<10:i++)
{
    fetch(url, {method:"HEAD"});
}

aborting is for when you want to terminate the call even if it hasn't been sent

as mentions by @Bergi , if you just want to ping the url then you can use

navigator.sendBeacon(url) 

but this will send a post request giving you much less control of what you do with the request

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