简体   繁体   English

Javascript等待函数响应完成

[英]Javascript wait for a function response to complete

I am trying to ask a follow up question "quick reply" in a Facebook bot implementation. 我试图在Facebook机器人实现中提出一个后续问题“快速回复”。 You can see the full app.js (sample file here): https://github.com/fbsamples/messenger-platform-samples/blob/master/node/app.js 您可以看到完整的app.js(此处为示例文件): https : //github.com/fbsamples/messenger-platform-samples/blob/master/node/app.js

When the 1) user enters a command -- eg "hello", after i send the user the relevant response -- 2) i want to ask a follow up question.. eg "Would you like to know about the weather today?" 当1)用户输入命令(例如“你好”)时,我向用户发送了相关的响应之后; 2)我想问一个后续问题。例如:“你想知道今天的天气吗?”

case 'hello':
       sendHelloGenericReponse(senderID);
       sendWeatherQuickReplyQuestion(senderID); //only execute this after above is complete
       break;

Ie i want to wait for 1 to complete before doing 2. However, for some reason the weather question always gets displayed to user first. 即我想等待1完成再执行2。但是,由于某种原因,天气问题总是首先显示给用户。 Any help would be appreciated 任何帮助,将不胜感激

wrap you function in setTimeout 在setTimeout中包装功能

 case 'hello': sendHelloGenericReponse(senderID); setTimeout(function(){ sendWeatherQuickReplyQuestion(senderID); },0) //only execute this after above is complete break; 

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

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