如何正确使用 Alexa 技能中的异步功能? [英] How to use async functions within Alexa skill properly?
问题描述
我对异步编程非常陌生,所以请原谅我缺乏理解,但我目前正在构建一个调用私人停车 API 的 Alexa 技能.您可以调用此 API,它会为您提供最近的停车位.
I am very new to async programming, so forgive me for the lack of understanding, but I am currently building an Alexa skill which calls a private parking API. You can call this API and it will give you the nearest parking spots.
const getParkingSpots_Handler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'getParkingSpots' ;
},
handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
const responseBuilder = handlerInput.responseBuilder;
let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let requestData = {
// I can't show this sorry
}
let options = {
// I can't show this sorry
};
// Call to the API
const postAxios = async () => {
try {
const response = await axios.post(API_URL, requestData, options);
return response.data.result;
} catch(error) {
console.log(error);
}
};
// Another function. This is where I use the data from the API response. I intent to add some code here that only picks out a number of results, sorts it by price etc. etc.
const useTheResult = async () => {
const result = await postAxios();
console.log('Response from the API:', result);
};
// We defined the functions above, now we need to execute them
useTheResult();
// This is what we will refer to the 'problem code'.
let say = `Hello from confidientialCompany! You can park...`;
return responseBuilder
.speak(say)
.reprompt('try again, ' + say)
.getResponse();
},
};
理想情况下,一旦我添加了代码来修改 useTheResult
中的响应,我希望问题代码也包含在 useTheResult
中......为什么?因为一旦我选择了我想要的数据并对其进行了修改,我就会尝试将 say 变成一个Alexa 可读"的句子,例如:
Ideally once I add the code to modify the response within useTheResult
, I want the problem code to be inside useTheResult
as well...why? Because once I've picked out the data I want and modified it, I'll try to turn say into an 'Alexa-readable' sentence like:
let say = `Hello from confidentialCompany! You can park on ${roadName1}, ${roadName2} and ${roadName3}. Prices start from ${startingPrice} pounds.`
如果我现在这样做,那么在 Alexa 控制台中测试时会出现错误.我不知道该怎么办了,我觉得我将陷入异步函数的无限循环中.
If I do that right now, as it is, I get an error when testing it out in the Alexa console. I have no idea what to do anymore, and I feel like I'm going to get stuck in an infinite loop of async functions.
推荐答案
在 handle
方法名中添加 async
关键字并在里面使用 await
:
Add async
keyword to handle
method name and use await
inside:
const getParkingSpots_Handler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest' && request.intent.name === 'getParkingSpots' ;
},
async handle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
const responseBuilder = handlerInput.responseBuilder;
let sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
let requestData = {
// I can't show this sorry
}
let options = {
// I can't show this sorry
};
// Call to the API
let result = null;
try {
const response = await axios.post(API_URL, requestData, options);
result = response.data.result;
} catch(error) {
// handle this case and return some message to User
console.log(error);
}
// assume your data structure to be like:
/**
result: {
roadName1: "1st street",
roadName2: "2nd street",
roadName3: "3rd street",
startingPrice: "1.2"
}
*/
const {roadName1, roadName2, roadName3, startingPrice} = result;
// This is what we will refer to the 'problem code'.
let say = `Hello from confidentialCompany! You can park on ${roadName1}, ${roadName2} and ${roadName3}. Prices start from ${startingPrice} pounds.`;
return responseBuilder
.speak(say)
.reprompt('try again, ' + say)
.getResponse();
},
};
如果你想在同一个函数中进行更多的调用:
If you want to do more calls in the same function:
try {
const [response1, response2] = await Promise.all([
axios.post(API_URL1, requestData, options),
axios.post(API_URL2, requestData, options)
]);
// do things with your responses
// ...
} catch(error) {
// handle this case and return some message to User
console.log(error);
}
这篇关于如何正确使用 Alexa 技能中的异步功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!