2017-01-03 69 views
1

我正在使用AWS Lambda開發我的第一個Alexa技能。 我面臨的挑戰是Lambda函數在所有同步函數運行之前完成。 我使用下面的例子 其中一個功能的()調用B()(我相信這將是默認異步調用) 當我測試機能的研究,有時我只得到輸出 「A完成(從here拍攝) ' 和函數返回而不執行B(),C(),D()AWS Lambda函數在IntentHandler調用的函數完成之前返回

我從我的GetUpdateIntent調用A()。我讀過幾篇文章,建議在回調 中使用context.done()以確保回調完成。我無法遵循如何實現這一點。 我喜歡我所有的回調函數和異步調用在lambda函數完成之前完成。

var A = function() { 
    return new Promise(function(resolve, reject) { 
     var result = 'A is done' 
     console.log(result) 
     resolve(result);  
    }) 
} 

var B = function() { 
    return new Promise(function(resolve, reject) { 
     var result = 'B is done' 

     setTimeout(function() { 
      console.log(result) 
      resolve(result); 
     }, 2000) 
     global_context.done(); 
    }) 
} 

var C = function() { 
    return new Promise(function(resolve, reject) { 
     var result = 'C is done' 
     console.log(result) 
     resolve(result); 
    }) 
} 

var D = function() { 
    return new Promise(function(resolve, reject) { 
     var result = 'D is done' 
     console.log(result) 
     resolve(result); 
    }) 
} 

A() 
.then(function(result) { 
    return B(); 
}) 
.then(C) 
.then(D) 
.then(console.log("All done")); 

Example.prototype.intentHandlers = { 
    GetUpdateIntent: function(intent, session, response){ 
     A(); 
    }, 
}; 

exports.handler = function(event, context) { 
    var skill = new Example(); 
    skill.execute(event, context); 
}; 

回答

1

您不在GetUpdateIntent函數中調用其餘的函數。您需要像設置示例類一樣將功能鏈接在一起

相關問題