我试图在从API调用获得响应后调用函数。喜欢这个
function saveScore(bot,result,message){
getFirstScore(bot,result).then(function(fscore){ //API -1
if(fscore){
getSecondScore(bot,result,null).then(function(sscore){ //API -2
if(fscore && sscore){
var finalScores = "First score is " + fscore + " :innocent:" + "\n Second score is "+ sscore +" :blush: ";
var remindAttachment = util.reminderAttachment("",finalScores);
listAllScores(bot,message,remindAttachment);
}
},function(error){
console.log('sscore error: ' + error);
});
}
},function(error){
console.log('fscore error: ' + error);
});
}
function listAllScores(bot,message,remindAttachment){
sendInstructions = function(response, convo){
convo.say(remindAttachment);
convo.say("Take it to the next level:");
convo.next();
setTimeout(function(){
listScores(bot,message);
},2000);
}
bot.startPrivateConversation(message, sendInstructions);
}
在这里,我想在 convo.say(remindAttachment); 之后调用 listScores(bot,message); 函数。但是现在它首先加载 listScores(bot,message); 。所以,我在 listScores(机器人,消息); 设置超时(不是一个好的方法),以便在一段时间后加载。
有没有更好的方法在 convo.say(remindAttachment)之后调用 listScores(bot,message); 功能;
修改
function listAllScores(bot,message,remindAttachment){
sendInstructions = function(response, convo){
convo.say(remindAttachment, function(error){
console.log("error ::::::::::::::::::::",error);
if(error!=null) {
listScores(bot,message);
}
});
}
bot.startPrivateConversation(message, sendInstructions);
}
答案 0 :(得分:0)
修改此convo.say()函数以将回调作为函数参数,然后在该回调调用" listScores(bot,message)"功能。 像这样 convo.say(remindAttachment,function(error){ if(错误!= null){ listScores(BOT,消息) } })