简体   繁体   English

从服务器获取数据随机停止获取

[英]Fetching data from server randomly stops fetching

I have something very strange happening in my program.我的程序中发生了一些非常奇怪的事情。 I have consol.logged the crap out of it with timestamps to try and figure out what is happening.我已经使用时间戳 consol.logged 废话,试图弄清楚发生了什么。 Basically my program randomly stops fetching data.基本上我的程序随机停止获取数据。 I have ensured a new stream of data is there but I have to refresh the entire page or resave the program to restart everything when it gets hung up.我已经确保有新的数据流存在,但我必须刷新整个页面或重新保存程序以在挂断时重新启动所有内容。 On top of that, there are no errors or flags telling me why it stops.最重要的是,没有错误或标志告诉我它为什么停止。 I tried to isolate the issue but it is something to do with the async function most likely.我试图隔离这个问题,但这很可能与 async 函数有关。 Here is the code....这是代码....

function App() {

  const data = async() => {
    try {
        console.log('try block initiated')
        const apiResponse = await fetch(ipAddress)
        console.log(apiResponse);
        console.log("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
        const responseText = await apiResponse.text();
        console.log(responseText)
        console.log("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC")
        if (loading === true){
            setLoading(false);
        }
        console.log("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDD")
        return responseText;
    } catch (error) {
        console.log('catch initiated')
        setError(true);
    }
  };
  console.log("AAAAAAAAAAAAAAAAAAAAAAA")
  try{
      console.log("EEEEEEEEEEEEEEEEEEEEEEEEEEE")
    data().then(response=>setMoisture(response));
    console.log("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
  } catch(error){
    console.log('gotcha')
    setError(true);
}
  
  let value = moisture;
  console.log(value);
  count += 1;  
  console.log(count);


  return(
    <div className="App">
      <WeatherCard moisture={value}/>    
    </div>
  );
}

Here is what the console looks like right before it stop fetching new data...这是控制台在停止获取新数据之前的样子...... 在此处输入图片说明

HERE IS A POSSIBLE SOLUTION.这是一个可能的解决方案。 After many headaches and researching async functions.经过多次头痛和研究异步函数。 I think this is going to work.我认为这会奏效。 I am going to let this run all night and see if it ever gets caught up but so far, Here is the solution...我要让它整夜运行,看看它是否会被赶上,但到目前为止,这是解决方案......

const data = async() => {
try {
    const apiResponse = await fetch(ipAddress)
    const responseText = await apiResponse.text();
    console.log("CCCCCCCCCCCCCCCCCCC");
    console.log(responseText);
    if (loading === true){
        setLoading(false);
    }
    data().then(response=>setMoisture(response));
    return responseText;
} catch (error) {
    console.log("EEEEERRRRRRRRRROOOOOOOOOOORRRRRRRRRRRR")
    setError(true);
    }   
};
console.log("AAAAAAAAAAAAAAAAAAAA");
if (moisture === "initialState"){
    data().then(response=>setMoisture(response));
}else{
    console.log("QQQQQQQQQQQQQQQQQQQQQQQQ")
}

Here is what I changed... I made an initial state called "initialSate and if the variable was this value, it runs the async function. I then NEVER CALL THIS ASYNC AGAIN OUTSIDE OF THE ASYNS ITSELF. Instead I call itself right before returning a value from the function. This works because my program is running while the async function is running. However, the program is sequential to itself and the async is sequential to itself. They are NOT sequential together. So, the async function now only gets called again once the async function finishes its first run. Simultaneaously the rest of my program is running. Before I think it was getting caught because my program was trying to call the async function before it had finished the first time. This is a pure guess because I have no way to proving that but it makes sense. If this overnight run fails. I will pull this from the solution. Otherwise, I think this will work. FINGERS CROSSED!这是我更改的内容......我创建了一个名为“initialSate 的初始状态,如果变量是这个值,它运行异步函数。然后我永远不会在 ASYNS 本身之外再次调用这个 ASYNC。相反,我在返回之前调用它自己函数中的一个值。这是有效的,因为我的程序正在运行,而异步函数正在运行。但是,程序本身是顺序的,而异步是自己顺序的。它们不是顺序在一起的。所以,异步函数现在只得到异步函数完成第一次运行后再次调用。同时我的程序的其余部分正在运行。在我认为它被捕获之前,因为我的程序试图在它第一次完成之前调用异步函数。这是一个纯粹的猜测因为我无法证明这一点,但这是有道理的。如果夜间运行失败。我将从解决方案中提取它。否则,我认为这会起作用。手指交叉!

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

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