从promise.then()方法返回handler.ResponseBuilder [英] Returning handler.ResponseBuilder from promise.then() method
问题描述
在我的Alexa技能的意图处理程序之一中,一旦我的诺言被兑现,我就必须返回响应。
In one of the intent handler for my Alexa skill I have to return response once my promise is resolved.
代码如下:
var rcvPromise = receiveMsgQ();
rcvPromise.then(function(speechText) {
console.log('rcv Promise resolved with ',speechText);
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('skill_name', speechText)
.withShouldEndSession(false)
.getResponse();
});
技能返回错误消息,没有有关错误的其他详细信息。
Skill returns with ERROR message with no additional details about error.
有什么办法可以解决此问题?
Is there any way to fix this problem?
PS:我需要使用promise作为 receiveMsgQ()
是异步函数调用。
PS: I need to use promise as receiveMsgQ()
is asynchronous function call.
推荐答案
您看到的错误是因为NodeJS和Alexa SDK是异步的。您可以从 Alexa SDK代码,它会调用您的请求处理程序,并希望返回Promise。在您的示例中,由于您的代码在调用 rcvPromise.then
之后没有显式返回任何内容,因此将空响应发送回,并且SDK将空响应发送给Alexa,从而导致错误。当您的 then()
函数将被执行时,Alexa响应已经发送,并且您的 handlerInput.responseBuilder
结果为
The error you're seeing is because NodeJS and the Alexa SDK are asynchronous. As you can read from the Alexa SDK code, it invokes your request handlers and expects a Promise back. In your example, as your code does not return anything explicitly after calling rcvPromise.then
, an empty response is sent back, and the SDK sends an empty response to Alexa, causing the error. When your then()
function will be executed, the Alexa response has already been sent and your handlerInput.responseBuilder
result is ignored.
要解决此问题,您有两种解决方案:
To solve this problem, you have two solutions:
a /您可以插入<$然后在 rcvPromise.then
之前的c $ c> return 语句,例如
a/ you can insert a return
statement before rcvPromise.then
, such as
return rcvPromise.then(function(speechText) {
console.log('rcv Promise resolved with ',speechText);
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('skill_name', speechText)
.withShouldEndSession(false)
.getResponse();
});
这样,您的处理程序将Promise返回给SDK,而SDK将使用
This way, your handler will return a Promise to the SDK and the SDK will use the result of the promise to build the response to send to Alexa.
b /如果您使用的是NodeJS 8,则可以使用新的 await
/ 异步
语法。在幕后,它与上面的解决方案相同,但是它将使代码更易于阅读。
b/ If you are using NodeJS 8, you can use the new await
/async
syntax. Under the hood, it is the same as the above solution, but it will make the code easier to read.
var speechText = await receiveMsgQ();
console.log('rcv Promise resolved with ',speechText);
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('skill_name', speechText)
.withShouldEndSession(false)
.getResponse();
请注意,您的整个处理函数必须标记为 async
才能正常工作。在 await
的更多信息。 async-await-in-nodejs / rel = noreferrer> https://blog.risingstack.com/mastering-async-await-in-nodejs/
Note that your entire handler function must be marked as async
for this to work. Read more about async
and await
at https://blog.risingstack.com/mastering-async-await-in-nodejs/
这篇关于从promise.then()方法返回handler.ResponseBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!