AWS Lambda函数在IntentHandler调用的函数完成之前返回

我正在使用AWS Lambda开发我的第一个Alexa技能。 我所面临的挑战是Lambda函数在所有同步函数运行之前完成。 我使用下面的例子(取自这里 )函数a()调用B()(我相信这将是默认asynchronous调用)当我testing我的function有时我只得到输出'A完成'和函数返回不执行B(),C(),D()

我从我的GetUpdateIntent调用A()。 我读过几个post,build议在callback中使用context.done()来确保callback完成。 我无法遵循如何实现这一点。 我喜欢所有的callback和asynchronous调用完成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); }; 

您不在GetUpdateIntent函数中调用其余的函数。 您需要像设置示例类一样将function链接在一起