简体   繁体   English

如何确保 function 1 在 function 2 开始之前在 useEffect 挂钩中结束?

[英]How can I make sure that function 1 ends before function 2 starts within useeffect hook?

I created a chatbot and want to initiate two Dialogflow API calls within a useEffect Hook.我创建了一个聊天机器人并想在 useEffect Hook 中启动两个 Dialogflow API 调用。 Function 1 and function 2 are both async api calls. Function 1 和 function 2 都是异步 api 调用。 The wanted result is that it first finishes function 1 and then starts with function 2, but currently function 2 returns the value faster than function 1.想要的结果是它首先完成 function 1,然后从 function 2 开始,但目前 function 2 比 function 1 返回值更快。

Do you have any idea how I can tell function 2 to wait until function 1 returns a value?你知道我如何告诉 function 2 等到 function 1 返回一个值吗?

useEffect(
    () => {
      createChatSessionId();
      fetchEventAnswerFromDialogflow("Startevent" + chatId, chatId); // function 1
      fetchEventAnswerFromDialogflow("Frageevent1", chatId); // function 2
    }, // eslint-disable-next-line
    []
  );

API Calls are asynchronous. API 调用是异步的。 When the compiler hits an async statement, it doesnt wait for it to finish, instead calls next statement.当编译器遇到异步语句时,它不会等待它完成,而是调用下一条语句。

Option one, (Using.then block):-选项一,(Using.then 块):-

  useEffect(
    () => {
      createChatSessionId();
      fetchEventAnswerFromDialogflow("Startevent" + chatId, chatId).then(rep=>{
      fetchEventAnswerFromDialogflow("Frageevent1", chatId);})
        .catch(err){
          console.log(err)
         }
    }, // eslint-disable-next-line
    []
  );

Option two - As per ES6 using async await.选项二 - 根据 ES6 使用异步等待。 I'd suggest not to make useEffect callback as async, instead make a new async function inside useEffect and call that.我建议不要将 useEffect 回调设为异步,而是在 useEffect 中创建一个新的异步 function 并调用它。

useEffect(
    () => {
      createChatSessionId();
      const getData = async() =>{
         await fetchEventAnswerFromDialogflow("Startevent" + chatId, chatId);
         await fetchEventAnswerFromDialogflow("Frageevent1", chatId);  //Can remove await 
             //here, depending upon usecase
      }
      getData();
    }, // eslint-disable-next-line
    []
  );

暂无
暂无

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

相关问题 如何在 Next JS 项目中使用 useEffect Hook 仅渲染一次 Google Translate Widget - How can I render Google Translate Widget only once using useEffect Hook in Next JS Project 警告在 useEffect() 中进行清理 function 偶尔会发生 - Warning to make a cleanup function in useEffect() occurs occasionally 如何让我的程序在执行 function 之前等待 Firebase 的数据? - How can make my Program wait for Firebase's data before executing a function? 如何确保发送和接收 Twilio Video dataTrack 消息? - How can I make sure the Twilio Video dataTrack message gets sent and recieved? 在哪里可以找到 firebase 云 function 部署日志? 路径以 /www-data-home/.npm/_logs/ 开头 - Where can I find firebase cloud function deploy logs? Path starts with /www-data-home/.npm/_logs/ 无法执行 lambda function。确保您已授予 CloudWatch Logs 执行您的 function 的权限 - Could not execute the lambda function. Make sure you have given CloudWatch Logs permission to execute your function 如何以正确的方式编写 function? - how can I write a function in proper way? 我在搜索 function 时收到无效挂钩调用错误 - I receive an invalid hook call error in the search function 我怎样才能调用该函数一次? - How can I call the function once? 组件“div”的视图配置 getter 回调必须是 function(收到“未定义”)。 确保以大写字母开头的组件名称 - view config getter callback for component 'div' must be a function (received 'undefined'). Make sure to start component names with a capital letter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM