Jovo框架下嵌套Promise调用后无法向Alexa/Google Assistant返回响应求助
问题分析与解决方案
看起来你遇到的核心问题是Jovo框架没有等待你的异步Promise链完成,就提前结束了请求上下文,导致你虽然成功获取了API数据,但调用this.ask()时已经无法向用户发送响应了。
问题根源
你的代码里使用了嵌套的Promise,但在第一个.then()中没有返回内部的meetup.GetMeetupEvent()的Promise。这会导致外层的Promise在启动内部异步请求后就立即resolve,Jovo会认为你的请求处理逻辑已经完成,提前关闭了请求上下文——这时候再调用this.ask()自然无法生效。
另外,如果你在请求处理器函数中没有返回整个Promise链,Jovo同样不会等待异步操作完成。
修复后的代码
我们可以把嵌套的Promise改成链式调用,并确保每一步都返回Promise,同时让处理器函数返回整个链:
// 假设这是你的意图处理器函数 MyMeetupIntent() { const city = this.$inputs.city.value; // 示例获取城市参数 // 必须return整个Promise链,让Jovo等待异步操作完成 return meetup.GetMeetupGroup(city) .then((details) => { const id = details.next_event.id; console.log(id); // 确认ID获取正常 // 返回内部Promise,让外层链等待它完成 return meetup.GetMeetupEvent(city, id); }) .then((event) => { const response = { speech: `<speak>The next learn to code meetup is ${event.name}. It will be held on <say-as interpret-as="date">${event.local_date}</say-as> at ${event.local_time} at ${event.venue.name}, which is located at <say-as interpret-as="address">${event.venue.address_1}</say-as>. ${event.plain_text_description}. Would you like to <say-as interpret-as="characters">RSVP</say-as>?</speak>`, reprompt: `<speak>The next ${city} meetup will be on <say-as interpret-as="date">${event.local_date}</say-as> at ${event.local_time} at ${event.venue.name}, <say-as interpret-as="address">${event.venue.address_1}</say-as>. Do you want to go?</speak>` }; console.log(response); // 确认响应内容正常 return response; }) .then((res) => { console.log(res); // 此时请求上下文仍处于活跃状态,this.ask可以正常发送响应 this.ask(res.speech, res.reprompt); }) .catch((error) => { console.log('Meetup API Error'); console.log(error); this.tell('Sorry, your request could not be completed at this time.'); }); }
关键修复点
- 返回内部Promise:在第一个
.then()中返回meetup.GetMeetupEvent()的Promise,确保整个Promise链是连贯的,Jovo会等待所有异步步骤完成。 - 处理器返回整个链:让你的意图处理器函数返回整个Promise链,告诉Jovo不要立即结束请求,要等待异步操作完成后再处理响应。
- 避免嵌套Promise:改用链式调用让代码更清晰,也更容易排查Promise链的问题。
这样修改后,你的this.ask()就能在请求上下文活跃的状态下被调用,响应就能正常发送给用户了。
内容的提问来源于stack exchange,提问作者Kevin Glick
相关产品推荐
相关产品推荐

