【发布时间】:2018-11-03 18:43:25
【问题描述】:
我将 AWS lambda 函数与 NodeJS 一起用于我的 Alexa 技能。但我坚持使用异步功能。
这是要求
- 启动时,会调用异步函数
function1 -
function1完成后,将结果传递给异步function2。 -
function2的结果应该告诉用户
所以我试图通过使用Promise 链接来解决这个问题。下面是我的伪代码
function function1() {
return new Promise((resolve, reject) => {
//some async operation
if (result1) {
resolve(result1);
} else {
reject(error);
}
});
}
function function2(result1) {
return new Promise((resolve, reject) => {
//some async operation
if (result2) {
resolve(result2);
} else {
reject(error);
}
});
}
const mainHandler = {
'LaunchRequest': function () {
function1()
.then(result1 => function2(result1))
.then(result2 => {
//succcess
this.response.cardRenderer(result2);
this.response.speak(result2);
this.response.listen(config.HELP_REPROMPT);
this.emit(':responseReady');
}).catch(err => {
//error
this.response.cardRenderer(err);
this.response.speak(err);
this.response.listen(config.HELP_REPROMPT);
this.emit(':responseReady');
});
},
};
现在我面临的问题是,alexa 在第一个函数执行后提前终止,而没有等待 function2 被执行。不知道我在这里做错了什么。任何帮助,将不胜感激。提前致谢。
注意:如果只有一个异步函数,那么它可以正常工作。即下面的代码工作正常。
const mainHandler = {
'LaunchRequest': function () {
function1()
.then(result1 => {
//succcess
this.response.cardRenderer(result1);
this.response.speak(result1);
this.response.listen(config.HELP_REPROMPT);
this.emit(':responseReady');
})
.catch(err => {
//error
this.response.cardRenderer(err);
this.response.speak(err);
this.response.listen(config.HELP_REPROMPT);
this.emit(':responseReady');
});
},
};'
问题在于涉及 2 个异步函数时
【问题讨论】:
标签: javascript node.js aws-lambda alexa alexa-skills-kit