Building an Alexa skill that requires persistence.
I am calling the database to get the user details prior to managing the flow and I'm struggling to get Node to wait for the response from DynamoDB (using Dynasty.js to handle the db connectivity).
I've tried a whole host of different promise/callback/node "blocking"/async approaches and the best I can do is have the response (in CloudWatch logs) appear when the user quits the skill. I'd really like the user configuration to happen at the start of the process, rather than at the end!
var credentials = {
accessKeyId: process.env.MY_ACCESS_KEY_ID,
secretAccessKey: process.env.MY_SECRET_ACCESS_KEY
};
var dynasty = require('dynasty')(credentials);
var tableName = dynasty.table('dynamoTable');
const promiseFunc = new Promise((resolve,reject)=>{
var myUser = tableName.find(userId);
setTimeout(_=>{resolve(myUser)}, 2000);
});
var checkUser = async function(){
if (true) {
console.log('check, check, 1, 2, 3');
await promiseFunc.then(function(myUser) {
if (myUser) {
console.log("USER FOUND");
console.log(myUser);
} else {
console.log("no user! sigh deeply and start again");
}
})
}
console.log("do more stuff now");
}
exports.handler = function(event, context) {
userId = event.session.user.userId;
const alexa = Alexa.handler(event, context);
// only check the first time, and only once the userId is properly constructed
if (!(userId.startsWith('amzn1.ask.account.testUser')) && (checkedUser != 1)) {
checkedUser = 1;
checkUser();
}
alexa.resources = languageString;
alexa.registerHandlers(newSessionHandlers, startStateHandlers, triviaStateHandlers, helpStateHandlers, stopStateHandlers);
alexa.execute();
};
How do I get node to wait for the response from dynamoDB before the rest of the script runs...