0

I have a similar problem like Use Async Functions in DialogFlow WebHook but that solution, changing request-promises for request-promises-native, didn't work for me, the difference it's that I'm using actions-on-google lib with ActionsSDK instead DialogFlow one, here is my code:

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);
}); 

If I change the call function for this one:

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

It works like a charm. I don't understand where I'm wrong, I'm returning promises until the intent. Any idea?

Thanks!

lezepo
  • 1
  • 1

1 Answers1

0

you can resolve this problem using async/await. It will look like this. It might help you.

(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

    })();
narayansharma91
  • 2,273
  • 1
  • 12
  • 20
  • If I change the call function to this one: function call() { let prom = new Promise((resolve,reject) =>{ resolve(); }); return prom; } It works, so I think the problem it's how to handle the request – lezepo Aug 21 '18 at 14:38
  • This should work, but it may be useful enough just to add some `return` statements in front of each promise, ie. `return handleAction` and `return call()` – Nick Felker Aug 21 '18 at 18:01
  • Works returning the functions (promises), what confuses me was that returning the new Promise works, should be because there wasn't nothing asyncronous inside( the request call). Thanks! – lezepo Aug 21 '18 at 19:12
  • Answer has been edited. Please check it might help you. – narayansharma91 Aug 22 '18 at 09:23
  • request method is asynchronous. It will give you result on callback. if you want to value without callback you can use promise. – narayansharma91 Aug 22 '18 at 09:27