How to use separate handlers with async await?


#1

Hello,

I am trying to use separate handlers with async await but Jovo seems to send back an empty response. Am I doing something wrong ?

Here are sample files from the helloworld template that shows the issue :

// app.js
'use strict';

const { App } = require('jovo-framework');

const { Alexa } = require('jovo-platform-alexa');

const { GoogleAssistant } = require('jovo-platform-googleassistant');

const { JovoDebugger } = require('jovo-plugin-debugger');

const { FileDb } = require('jovo-db-filedb');

// ------------------------------------------------------------------

// APP INITIALIZATION

// ------------------------------------------------------------------

const app = new App();

app.use(new Alexa(), new GoogleAssistant(), new JovoDebugger(), new FileDb());

app.setHandler(require('./stateless.js'), {

StateOne: require('./state-one.js'),

});

module.exports = { app };


// stateless.js
module.exports = {
  LAUNCH() {
    this.toStateIntent('StateOne', 'GetNameIntent');
  },

  Unhandled() {
    this.toIntent('LAUNCH');
  },
};


// state-one.js
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function getName() {
  await delay(500);
  return 'John';
}

module.exports = {
  GetNameIntent: async function () {
    const name = await getName();
    console.log({ name });
    return this.tell('Hey ' + name + ', nice to meet you!');
  },

  Unhandled() {
    this.toIntent('LAUNCH');
  },
};

#2

I was missing the return in stateless.js

// stateless.js
module.exports = {
  LAUNCH() {
    return this.toStateIntent('StateOne', 'GetNameIntent');
  },

  Unhandled() {
    this.toIntent('LAUNCH');
  },
};

#3

This fixed your issue, right?

btw. Welcome to the Jovo Community :slight_smile: