2017-01-25 17 views
0

我想從API調用獲取響應後調用一個函數。像這樣Slack API處理兩個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); 
} 

在這裏,我想打電話listScores(bot,message);功能convo.say(remindAttachment);。但現在它正在加載listScores(bot,message);首先是。所以,我爲listScores(bot,message)設置超時(不是一個好的方法);在某個時間後加載。

有沒有更好的方法來呼叫listScores(bot,message);功能convo.say(remindAttachment);

編輯

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

修改此convo.say()函數以具有回調作爲函數參數,然後內部的回叫呼叫 「listScores(BOT,消息)」 的功能。 這樣 convo.say(remindAttachment,函數(誤差){ 如果(錯誤!= NULL){ listScores(BOT,消息) } })

+0

感謝答覆。嘗試使用回調,但該回調函數沒有被調用。 – NNR

+0

你能給我convo.say函數的片段嗎? –