我对异步编程很陌生,所以请原谅我缺乏理解,但我目前正在构建一个调用私人停车 API 的 Alexa 技能。您可以调用此 API,它会为您提供最近的停车位。
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 可读”的句子,例如:
let say = `Hello from confidentialCompany! You can park on ${roadName1}, ${roadName2} and ${roadName3}. Prices start from ${startingPrice} pounds.`
如果我现在这样做,那么在 Alexa 控制台中进行测试时会出错。我不知道该做什么了,我觉得我会陷入异步函数的无限循环中。