发送请求后以及等待/下载时间之前流产JS fetch()
我的问题:
我当前正在尝试将几个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那就是
本来要做的,无需参与sendbeAcon
sendbeaconfetch
。如果那不符合您的要求,您仍然可以使用
fetch
和根本不等待响应 - 没有任何迫使您使用等待
。不。无论如何,流产的请求不是正确的解决方案。请注意,即使仅在发送HTTP请求后仅关闭连接时,服务器仍可能会注意到这一点,并且不会在没有可写的目的地进行响应目的地处理请求。
That's what
sendBeacon
is meant to do, no need to involvefetch
.If that doesn't meet your requirements, you can still use
fetch
and simply not wait for the response - nothing forces you to useawait
.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.
如果您想发送请求但不需要回复正文,则应使用 HEAD 方法发送,这指示服务器仅回复调用的标头而不是正文,因此您可以检查状态或标头包中的内容类型等
尽快发送 10 个请求,然后等待响应
,或者如果您真的不关心响应,则无需等待承诺完成,
中止是在您想要终止时使用的呼叫即使它 @Bergi尚未提及
,如果您只想 ping 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
or if you really don't care about the response as all there is no need to await the promise completion
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
but this will send a post request giving you much less control of what you do with the request