简体   繁体   English

使用setTimeout()的Promise.all()问题,状态不更新

[英]Promise.all() problem using setTimeout(), state not updating

I have an arrow function that is invoked from cDM to retrieve the updated status of schedules every 20 seconds with a setTimeout() . 我有一个箭头函数,从cDM调用,使用setTimeout()每20秒检索一次更新的调度状态。

componentDidMount() {
    //get request to /schedules
    //update state with response data
    this.getUpdatedStatus();
}

Each schedule present at /schedules looks like this: /schedules每个时间表如下所示:

"data": {
"id": "2147483605",
"selfUri": "/schedules/2147483605",
"type": "Schedule",
"status": "Pending",
}

So in the below method, each schedule.selfUri is requested, and I'm attempting to update status of each schedule. 因此,在下面的方法中,请求每个schedule.selfUri ,并且我正在尝试更新每个计划的status

    getUpdatedStatus = () => {
//fetch updated status,
const schedules = this.state.schedules;
Promise.all(
  schedules.map(schedule =>
    axios({
      method: "get",
      url: schedule.selfUri,
    })
  )
)
  .then(response => {
    console.log(response);
    const isIncomplete = response.some(r => r.status !== "Complete");
    console.log(isIncomplete);
    if (isIncomplete) {
      this.timeout = setTimeout(() => this.getUpdatedStatus(), 20000);
    }
    this.setState(
      {
        scheduleStatus: isIncomplete ? "Pending" : "Complete",
      },
      () => {
        console.log(this.state.scheduleStatus);
        console.log(this.state.schedules);
      }
    );
  })
  .catch(error => console.log(error.response));

}; };

The setTimeout function is working and requesting every 20 seconds to retrieve a possible update on the status. setTimeout函数正在工作,并请求每20秒检索一次可能的状态更新。 The object response eventually returns a status of complete but the value is not re-rendering in my table. 对象响应最终返回complete状态,但该值不会在我的表中重新呈现。 I believe there is a problem with my promise chain and using setTimeout isn't updating my this.state.scheduleStatus when it completes. 我相信我的保证链存在问题,并且使用setTimeout在完成时不会更新我的this.state.scheduleStatus I have attached a codesandbox that gives a better view of my issue. 我附上了一个代码框,可以更好地查看我的问题。

Codesandbox Codesandbox

I don't think your problem has anything to do with the promise chain or using setTimeout() . 我不认为您的问题与promise链或使用setTimeout()有任何关系。 I think it's caused by incorrectly getting the value for the isIncomplete flag, that it always gets set to true . 我认为这是由错误地获取isIncomplete标志的值引起的,它总是被设置为true

You're setting this value by the following line: 您可以通过以下行设置此值:

const isIncomplete = response.some(r => r.status !== "Complete")

I think the problem here is that you're expecting the response object to be the data you've specified, which contains a status property with a string value, while in reality it's the response object returned by Axios, where the status property is the status code of the actual response ( 200 when successful). 我认为这里的问题是你期望响应对象是你指定的数据,它包含一个带有字符串值的status属性,而实际上它是Axios返回的响应对象,其中status属性是实际响应的状态代码(成功时为200 )。 Therefore, isIncomplete is always true because r.status never equals "Complete" . 因此, isIncomplete始终为true因为r.status永远不会等于"Complete"

Your data can be found in the data property and thus the aforementioned line should be like this: 您的数据可以在data属性中找到,因此上述行应该是这样的:

const isIncomplete = response.some(r => r.data.status !== "Complete")

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

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