在继续执行欢迎意图< speak>之前,如何等待此提取JSON请求。命令 [英] How to wait for this fetch JSON request before proceeding to the welcome intent <speak> command

查看:83
本文介绍了在继续执行欢迎意图< speak>之前,如何等待此提取JSON请求。命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用访存来获取json对象,但这确实可以正常工作。然后我根据数据分配一个字符串,并想在ssml响应中使用该字符串...按照我现在的方式进行操作,现在大约300毫秒太慢了,如果我尝试放置var,则var是不确定的该代码中其他地方的ssml响应会出现错误未设置响应。谁能指出正确的方向,我已经连续5天关注这个问题了(因为每次都要花费30秒钟以上的时间,因此已经放弃了在Firestore上进行数据检索)。

I'm trying to use fetch to get a json object, and this does work correctly. Then I'm assigning a string based on the data and want to use that string in the ssml response...doing it the way I have it now it is about 300 ms too slow and the var is undefined, if I try to place the ssml response elsewhere in this code it comes up with an error "No response has been set". Can anyone point me in the right direction, I've literally been on this issue for 5 days in a row (already gave up on Firestore for data retrieval as it was taking 30+ seconds each time anyway).

//this is what I have in the Welcome Intent -- I should note that all of this 
//is in a function called welcome() which is called by the welcome intent

function foo() {
  // RETURN the promise
  return fetch("https://webhostapp.com/townsheriff.json")
    .then(function(response){
      return response.json(); // process it inside the `then`
    });
}

foo().then(function(response){
  // access the value inside the `then`
  currentquestion = response[2].n111.toString(); //assigning variable works

  //I tried putting the ssml response here but then got "no response set"
  //error

})


//here it comes up as undefined because it happens 300ms too early
const ssml =
  '<speak>' +
    '<audiosrc="https://test.mp3">You have just been made sheriff...</audio>'+
    currentquestion  +
  '</speak>';

conv.ask(ssml);


推荐答案

问题是一切 >您要使用 fetch 处理API调用的结果,这需要作为Promise解析的一部分进行处理。 Promises的工作方式是,最初运行它之后的代码将继续执行,但是 then()块中的内容将在Promise完成时被调用。

The issue is that everything you want to do with the result of your API call with fetch needs to be handled as part of the Promise resolution. The way Promises work is that code after it will continue to be executed when it is initially run, but what is in the then() block will be called when the Promise completes.

另外,您还需要确保返回承诺,因此Intent Handler调度程序知道等待Promise完成。

Additionally, you need to make sure you return the promise as well, so the Intent Handler dispatcher knows to wait for the Promise to complete.

第一部分是通过放置所有内容来处理的,包括将对 conv.ask()的调用c $ c> then()部分。第二部分通过返回Promise处理。可能看起来像这样:

The first part is handled by putting everything, including the call to conv.ask() in the then() portion. The second part is handled by returning the Promise. It might look something like this:

// Make sure you return this promise
return foo().then(function(response){
  // access the value inside the `then`
  currentquestion = response[2].n111.toString(); //assigning variable works

  // Make sure you build the response, and ask it inside the promise resolution
  const ssml =
    '<speak>' +
    '<audiosrc="https://test.mp3">You have just been made sheriff...</audio>'+
    currentquestion  +
    '</speak>';

  conv.ask(ssml);
});

这篇关于在继续执行欢迎意图&lt; speak&gt;之前,如何等待此提取JSON请求。命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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