I am using AWS lambda function with NodeJS for my alexa skill. But I am stuck in using async function.
Here is the requirement
- On launching, a asynchronous function
function1is called - After the completion of
function1, the result is passed to asyncfunction2. - The result of
function2should 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