2017-07-11 71 views
1

我想了解如何使用Node.js回調具體爲什麼和什麼時候你會在Alexa技能中使用它們。 的highlow遊戲樣本https://github.com/alexa/skill-sample-nodejs-highlowgameuses採用回調時,正確的號碼已經猜到了,但 如果我將回調代碼到NumberGuessIntent功能功力似乎表現得完全一樣的,所以什麼是回調的目的是什麼?回調在Alexa技能中是否重要?

代碼,而不回調:

'NumberGuessIntent': function() { 
     var guessNum = parseInt(this.event.request.intent.slots.number.value); 
     var targetNum = this.attributes["guessNumber"]; 
     console.log('user guessed: ' + guessNum); 

     if(guessNum > targetNum){ 
      this.emit('TooHigh', guessNum); 
     } else if(guessNum < targetNum){ 
      this.emit('TooLow', guessNum); 
     } else if (guessNum === targetNum){ 
      this.handler.state = states.STARTMODE; 
      this.attributes['gamesPlayed']++; 
      this.emit(':ask', guessNum.toString() + 'is correct! Would you like to play a new game?', 
       'Say yes to start a new game, or no to end the game.'); 
     } else { 
      this.emit('NotANum'); 
     } 
    }, 

回答