简体   繁体   English

谁能告诉我为什么 try 块中的 setTimeout function 不起作用?

[英]Could anyone tell me why my setTimeout function within the try block does not work?

I have a problem.我有个问题。 Could anyone tell me why my setTimeout function within the try block does not work?谁能告诉我为什么 try 块中的 setTimeout function 不起作用? It doesn't wait for 10000 milliseconds and simple runs through.它不会等待 10000 毫秒并且简单地运行。 As a result, the console shows the error message "cannot read property data of "undefined". The API should return an object, but needs some time to fetch the answer. The console.log(responseInformation) returns also "undefined".结果,控制台显示错误消息“无法读取“未定义”的属性数据。API 应该返回 object,但需要一些时间来获取答案。console.log(responseInformation) 也返回“未定义”。

const fetchAnswerFromDialogflow = 
try {
      const res = await axios.post(
        `https://dialogflow.googleapis.com/v2/projects/myteachingbot-arxmxd/agent/intents:batchUpdate`,
        node,
        config
      );

      const waitForResponseAssignment = async () => {
        let nodesSavedToChatbot = await res.response.data;
        return nodesSavedToChatbot;
      };

      const responseInformation = setTimeout(
        await waitForResponseAssignment(),
        10000
      );
      
      console.log(responseInformation);

The problems in your code:您的代码中的问题:

const res = await axios.post(
  `https://dialogflow.googleapis.com/v2/projects/myteachingbot-arxmxd/agent/intents:batchUpdate`,
  node,
  config
);

const waitForResponseAssignment = async () => {
   /*
    axios response doesnt have a property called response,
    so res.response.data is error.
    Also res.data is already a response from the api(res is
    not a promise so you dont have to use await below, 
    even you dont need this `waitForResponseAssignment` function)
   */
  let nodesSavedToChatbot = await res.response.data;
  return nodesSavedToChatbot;
};

// This timeout function is not correct, setTimeout
// accepts the first param as callback(but you passed a value)
// Actually you also dont need this `setTimeout`
const responseInformation = setTimeout(
  await waitForResponseAssignment(),
  10000
);

You can just use the below code:您可以使用以下代码:


const res = await axios.post(
  `https://dialogflow.googleapis.com/v2/projects/myteachingbot-arxmxd/agent/intents:batchUpdate`,
  node,
  config
);
return res.data;

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

相关问题 TextGeometry没有显示,有人可以告诉我为什么吗? - TextGeometry not displaying, Could anyone tell me why? 我的代码在$(document).ready()函数中不起作用。 谁能帮我理解原因? - My code doesn't work within a $(document).ready() function. Can anyone help me understand why? 谁能告诉我为什么在尝试调用此函数时会出错? - Can anyone tell me why I get a error when i try to call this function? Javascript初学者-谁能告诉我为什么我的按钮不起作用? - Javascript beginner - Can anyone tell me why my button doesn't work? 谁能告诉我为什么我的导航不起作用? - Can anyone please tell me why my navigation doesn´t work? 为什么在try块中重新声明函数标识符会抛出SyntaxError? - Why does redeclaring a function identifier within a try block throw a SyntaxError? 为什么我的Web浏览器上的debugg系统告诉我(drawImage不是函数)? - Why does my debugg system on the webbrowser tell me (drawImage is not a function)? 当我将 animation 效果包装在 function 中时,它停止工作。 谁能告诉我为什么? - When I wrap my animation effect in a function, it stops working. Can anyone tell me why? 有人告诉我为什么这个Jquery在IE中不起作用吗? - Anyone tell me why this Jquery doesn't work in IE? 任何人都可以告诉我为什么我的脚本禁用了s键吗? - Can Anyone tell me why my script is disabling the s key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM