I have read this answer: How do I return the response from an asynchronous call?
I have used .done in the end and it does not work.
I am trying to return a value from a promise and store it after a promise is resolved.
If i console.log the result, it works but when I try to return the value, i get a pending promise.
function getpassphrase() {
return client.getItem('user-passphrase')
.setHashKey('user','-1035827551964454856')
.selectAttributes(['user', 'passphrase'])
.execute()
.then(function (data) {
return data.result.passphrase;
})
};
const y = getpassphrase()
.done(function(r) {
return r;
//if i do console.log(r) it gives the actual result
})
console.log(y);
I have tried async await as well:
const x = (async function(){
const y = await getpassphrase();
return x
})();
I run into the same issue.. the x value is a promise pending here..but console.log gives the actual value..
expected: 'abc' actual: 'undefined'
This goes into my alexa handler which when used inside a then function is throwing a 'unhandled response error'
const passPhraseIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'getPassPhraseIntent';
},
handle(handlerInput) {
async function getpassphrase(){
return (await client.getItem('user-passphrase')
.setHashKey('user','-1035827551964454856')
.selectAttributes(['user', 'passphrase'])
.execute()
.then(function (data) {
return data.result.passphrase;
}));
}
(async function(){
let passphrase_db = await getpassphrase();
return handlerInput.responseBuilder
.speak(speechText2)
.getResponse();
})();
}
};