2016-09-20 2 views
0

[Node.js ASK](https://developer.amazon.com/public/community/post/Tx213D2XQIYH864/Announcing-the-Alexa-Skills-Kit-for-Node-js)を使用してAPIからデータを返す単純なAlexaスキルを作成しようとしています。私はhttpをハンドラの中に入れますが、コールバックが非同期的にAPIデータを返す前に、Alexaはハンドラを完了します。Alexa Node.jsスキルキット - ハンドラが完了する前にコールバックデータを返す必要があります

私は答えを探してきた、と私の考えは、現在、次のとおりです。

  1. フィギュアアウト同期私は
  2. をしないのですデータ
  3. 簡単な何かを取得する方法を
  4. のNode.jsを使用しませんコードの

コア:

exports.handler = function(event, context, callback) { 
 
    var alexa = Alexa.handler(event, context); 
 
    alexa.registerHandlers(handler); 
 
    alexa.execute(); 
 
}; 
 

 
var handler = Alexa.CreateStateHandler(states.x, { 
 
    'intent': function() { 
 
    var options = { 
 
     host: baseURL, 
 
     path: pathURL 
 
    }; 
 
    
 
    callback = function(response) { 
 
     var str = ""; 
 
     response.on('data', function(piece) { 
 
     str += piece; 
 
     }); 
 
     
 
     response.on('end', function() { 
 
     //does not get executed 
 
     this.emit(':tell', str, "test"); 
 
     }); 
 
    } 
 
    
 
    http.request(options, callback).end(); 
 
    
 
    //this does get executed if I leave this here 
 
    this.emit(':tell'...); 
 
    };

答えて

0

あなたは範囲の問題があると思います。 try ...

response.on('end',() => { 
    this.emit(':tell', str, "test"); 
}); 
関連する問題