0
votes

I am using AWS lambda function with NodeJS for my alexa skill. But I am stuck in using async function.

Here is the requirement

  1. On launching, a asynchronous function function1 is called
  2. After the completion of function1, the result is passed to async function2.
  3. The result of function2 should be spoken out to user

So I am trying to solve this by using Promise chaining. Below is my pseudocode

function function1() {
    return new Promise((resolve, reject) => {
        //some async operation
        if (result1) {
            resolve(result1);
        } else {
            reject(error);
        }
    });
}

function function2(result1) {
    return new Promise((resolve, reject) => {
        //some async operation
        if (result2) {
            resolve(result2);
        } else {
            reject(error);
        }
    });
}

const mainHandler = {
    'LaunchRequest': function () {
        function1()
            .then(result1 => function2(result1))
            .then(result2 => {
                //succcess
                this.response.cardRenderer(result2);
                this.response.speak(result2);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady');
            }).catch(err => {
                //error
                this.response.cardRenderer(err);
                this.response.speak(err);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady');
            });
    },
};

Now the issue I am facing is that alexa terminates prematurely after first function execution without waiting for function2 to be executed. Not sure what I am doing wrong here. Any help would be appreciated. Thanks in advance.

Note: If there is only one async function then it works fine. i.e below code works fine.

const mainHandler = {
    'LaunchRequest': function () {
        function1()
            .then(result1 => {
                //succcess
                this.response.cardRenderer(result1);
                this.response.speak(result1);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady'); 
            })
            .catch(err => {
                //error
                this.response.cardRenderer(err);
                this.response.speak(err);
                this.response.listen(config.HELP_REPROMPT);
                this.emit(':responseReady');
            });
    },
};'

Issue is when 2 async functions are involved

1

1 Answers

0
votes

See if this helps:

function function1() {
    return new Promise((resolve, reject) => {
        //some async operation
        if (result1) {
            resolve(result1);
        } else {
            reject(error);
        }
    });
}

function function2(result1) {
    return new Promise((resolve, reject) => {
        //some async operation
        if (result2) {
            resolve(result2);
        } else {
            reject(error);
        }
    });
}

const ErrorHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'LaunchRequest';
  },
  async handle(handlerInput, error) {
    result1 = await function1();
    result2 = await function2(result1);
    /* rest of code according to version2 */
        },
    };

For v1 and v2 sdk difference click here.