从 promise.then() 方法返回 handler.ResponseBuilder [英] Returning handler.ResponseBuilder from promise.then() method

查看:24
本文介绍了从 promise.then() 方法返回 handler.ResponseBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 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前插入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();
 });   

这样,您的处理程序将向 SDK 返回一个 Promise,而 SDK 将使用该承诺的结果来构建发送给 Alexa 的响应.

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/async 语法.在底层,它与上面的解决方案相同,但它会使代码更易于阅读.

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 才能使其工作.在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆