简体   繁体   English

中止上一个后无法发出新的获取请求

[英]Can't make new fetch request after aborting previous

I need to change a parameters that defines what data should come from my requests, also this application needs to refresh every fixed interval of time.我需要更改一个参数,该参数定义了我的请求应该来自哪些数据,该应用程序还需要每隔固定的时间间隔刷新一次。 If the user changes the parameter of interest in the middle of a previous refresh request things start to behave strange and some unexpected behavior occurs.如果用户在之前的刷新请求中间更改了感兴趣的参数,事情就会开始表现得很奇怪,并且会发生一些意外的行为。

So my approach was to abort all previous requests before starting the new ones, but after using await controller.abort() it seems that the next requests are never triggered, Do I need to clear the signal or something like that?所以我的方法是在开始新请求之前中止所有以前的请求,但是在使用await controller.abort()之后,似乎永远不会触发下一个请求,我是否需要清除信号或类似的东西?

const controller = new AbortController();
const async fetchData = (url, body = null) => {
  let data;
  const signal = controller.signal;
  const headers = { ... };
  response = await fetch(url, body ? {
    method: "POST",
    body: JSON.stringify(body),
    signal,
    headers
  } : { headers, signal });;
  data = await response.json()
  return data
}

const firstData = await fetchData(url1, body1);
await controller.abort();
const secondData= await fetchData(url2, body2);

What happens is that secondData always is undefined , actually this second request never happens (looking on network traffic).发生的事情是secondData总是undefined ,实际上这第二个请求永远不会发生(查看网络流量)。 If I stop source and try to run await fetchData(url2) after .abort() has executed it prompts an erros saying that Uncaught SyntaxError: await is only valid in async function or if I try to run it without await it returns a pending promise, but the actual request is nowhere to be seen in traffic tab.如果我停止源并在.abort()执行后尝试运行await fetchData(url2) ,它会提示错误说Uncaught SyntaxError: await is only valid in async function或者如果我尝试在没有await的情况下运行它,它会返回一个挂起的 promise ,但实际请求在流量选项卡中无处可见。


Solved解决了

Applying what was suggested on the ansewr I created wrapper on the function, to call new controllers everytime.应用我在 function 上创建的包装器上的建议,每次都调用新的控制器。

let controller = null;
let fetchData = null;
const initializeFetchData = () => {
  const controller = new AbortController();
  const async fetchData = (url, body = null) => {
    let data;
    const signal = controller.signal;
    const headers = { ... };
    response = await fetch(url, body ? {
      method: "POST",
      body: JSON.stringify(body),
      signal,
      headers
    } : { headers, signal });;
    data = await response.json()
    return data
  }
}

initializeFetchData();
const firstData = await fetchData(url1, body1);
controller.abort();
initializeFetchData();
const secondData= await fetchData(url2, body2);

You are using the same AbortController for two different requests.您对两个不同的请求使用相同的AbortController After calling .abort() on the AbortController you have updated the state of it's AbortSignal which then renders the second request void.在 AbortController 上调用AbortController .abort()之后,您已经更新了它的 AbortSignal 的AbortSignal ,然后使第二个请求无效。

You should use a separate AbortController for each request if you want this behavior.如果您想要这种行为,您应该为每个请求使用单独的AbortController Of course, it is perfectly acceptable to reuse an AbortController for multiple fetch requests if you want to be able to abort all of them in one go.当然,如果您希望能够在一个 go 中中止所有请求,那么将AbortController用于多个fetch请求是完全可以接受的。

A couple of other points...其他几点...

  • .abort() is a synchronous method which returns void so you do not need the await prefix when calling .abort() . .abort()是一个同步方法,它返回void ,所以在调用.abort()时不需要await前缀。
  • In your code example, the first request will never be aborted as you are awaiting the fetch request, which will complete before the .abort() is called.在您的代码示例中,第一个请求将永远不会中止,因为您正在等待fetch请求,该请求将在.abort()被调用之前完成。

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

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