首页 > 解决方案 > 使用 actions-on-google node v2 请求承诺

问题描述

我有一个类似的问题,例如在 DialogFlow WebHook 中使用异步函数,但是该解决方案,更改 request-promises-native 的 request-promises,对我不起作用,不同之处在于我使用 Actions-on-google lib 和 ActionsSDK而不是 DialogFlow 之一,这是我的代码:

function call() {

  var options = {
      url: "https://google.es"
  };
  return request(options)
    .then((res) => {
        console.log("Success", res);
        Promise.resolve();
    })
    .catch((err) => {
        console.log("Error", err);
        Promise.resolve();
    });
}


const handleAction = (conv) => {

  call()
    .then(() => {
        console.log("Going to ASK");                                                                                                                                                                                                       
        conv.ask('Hi, how is it going?');
        return Promise.resolve();
    })
    .catch(error => {
        console.log("Ask ERROR");                                                                                                                                                                                                    
        conv.ask('Hi, how is it going?');
        return Promise.resolve();

    });
}

app.intent('actions.intent.MAIN', (conv) => {
    handleAction(conv);
}); 

如果我改变这个调用函数:

function call() {
  let prom =  new Promise((resolve,reject) =>{                                                                                                                                                                                                             
        resolve();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  });                                                                                                                                                                                                                                                     
  return prom;
}   

它就像一个魅力。我不明白我错在哪里,我正在返回承诺,直到意图。任何想法?

谢谢!

标签: node.jsactions-on-googlerequest-promise

解决方案


你可以使用 async/await 来解决这个问题。它看起来像这样。它可能会帮助你。

(async () => {
   async function call() {
    var options = {
           url: "https://google.es"
     };
    return await new Promise((resolve, reject) => {
          request(options)
            .then((res) => {
                resolve(res);
            })
            .catch((err) => {
                console.log("Error", err);
                reject(err)
            });
        }
    }

    const handleAction = await call(); //you will get result on handle Action varaible

    })();

推荐阅读