javascriptnode.jsasynchronousrequestjovo-framework

How do you (or can you) run asynchronous code in JOVO's Intents?


I want to run an asynchronous function inside of my JOVO's intent. At the moment, it's running the code synchronously, which doesn't allow the JOVO to send the correct response.

This is what my code looks like.

app.setHandler({
  Intent() {
    var response = 'nada';

    response = FindItem(this.$inputs.Item.value);
    //I want it to be like:
    //response = await FindItem(this.$inputs.Item.value);//Where FindItem is a asynchronous method

    this.ask(response);
  }
});

FindItem() returns a string of an item name (like Banana).

However, this just responds with 'nada'. I want it to respond with (ex:) 'banana'

Any solutions?

Thanks in advance!


Solution

  • Easy solution from JOVO's website

    Basically, you want to use either Request Promises or Promises in the FindItem method, and use 'async' and 'await' in the Intent.

    app.setHandler({
      **async** Intent() {
        var response = 'nada';
    
        response = **await** FindItem(this.$inputs.Item.value);
        //I want it to be like:
        //response = await FindItem(this.$inputs.Item.value);//Where FindItem is a asynchronous method
    
        this.ask(response);
      }
    });
    

    FindItem would look like:

    async Finditem(item) { ... }