如何在循环中使用 XMLHttpRequest.onload 并在使用响应表之后?

问题描述

我想使用 XMLHttpRequest 从 API 接收 JSON 文件。目前我需要进行 REST 调用 (XMLHttpRequest)。然后,使用这两个响应通过 chart.js 创建图表。

我唯一的问题是异步问题,我不知道如何避免。

我阅读了很多主题,但没有找到解决方案。

我通过以下链接做出承诺:How do I promisify native XHR?

你能帮我吗?

下面的代码是主函数中的代码(如你所见,tab[0] 将为空):

transmission9 %>% 
  mutate(across(everything(),~ match(.,unique(c(t(cur_data())))),.names = '{.col}_code'))

   In_Node End_Node In_Node_code End_Node_code
1 c4ca4238 2838023a            1             2
2 c4ca4238 d82c8d16            1             3
3 c4ca4238 a684ecee            1             4
4 c4ca4238 fc490ca4            1             5
5 28dd2c79 c4ca4238            6             1
6 f899139d 3def184a            7             8

下面的代码是 makeRequest 函数

let tab = []; // make empty table to save the two results in
/*Request to the API */
for (let countergraphic = 0; countergraphic < 2; countergraphic++) {
  let action = 'GET';
  let requestURL = 'http://127.0.0.1:5000/' + countergraphic + '/'; // sample : http://127.0.0.1:5000/0/
  makeRequest(action,requestURL,tab)
    .then(function(datums) {
      console.log(datums);
    })
    .catch(function(err) {
      console.error('Augh,there was an error!',err.statusText);
    });
}
let first_tab = tab[0];
console.log(tab[0]);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)