0

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();
    })();

  }
};
thealchemist
  • 397
  • 7
  • 24
  • 1
    `handle()` can be `async` https://developer.amazon.com/blogs/alexa/post/4a46da08-d1b8-4d8e-9277-055307a9bf4a/alexa-skill-recipe-update-call-and-get-data-from-external-apisblog or [callback](https://stackoverflow.com/questions/51764274/how-to-make-an-asynchronous-api-call-for-alexa-skill-application-with-a-lambda-f). Also `done()` is only for jQuery, just keep that in mind. – Alexander Staroselsky Aug 27 '19 at 20:06
  • I am unfortunately getting an alexa unhandled response error: handle(handlerInput) { (async function(){ let passphrase_db = await getpassphrase(); return handlerInput.responseBuilder .speak(passphrase_db) .getResponse(); })(); } – thealchemist Aug 27 '19 at 20:12
  • Just out of curiosity, why not do the process inside the ``then`` block itself? – Rohit Kashyap Aug 27 '19 at 20:20
  • i tried both async and then.. alexa returns an error if i send the response out from inside the then or async block. – thealchemist Aug 27 '19 at 20:24
  • I think you are misunderstanding how promises work, you can only access the resolved value after a then block and you cannot return from an async task. – Rohit Kashyap Aug 27 '19 at 20:29
  • i know this much that we have to use a then block and write everything to do with the promise return inside the 'then' block.. but i was wondering if there is any way to pull the return out to be used outside.. so i am concluding that there is no way to do this :) – thealchemist Aug 27 '19 at 20:48

3 Answers3

0

Use async-await inside an IIFE :

(async (){
  const y = await getpassphrase();
  console.log(y);
})();
Taki
  • 17,320
  • 4
  • 26
  • 47
  • I tried it.. console.log(y) works but if i try to do something like this: const x = (async function(){ const y = await getpassphrase(); return y })(); – thealchemist Aug 27 '19 at 19:58
  • Will wrapping the whole thing in a promise (inside the function) and resolving after the then block when the successful response is received work? After that, function call with a then block should work. – Rohit Kashyap Aug 27 '19 at 20:00
0

Your best bet is to use async await, which is the current standard for handling promises/asynchronous functionality.

Define the logic you have here within the context of an asynchronous function, and then use "await" to pause until completion of the promise and unwrap the value.

async function sample() {
    const y = await getpassphrase();
    console.log(y); //abc
}
Michael
  • 581
  • 2
  • 8
  • I am using alexa so I am not able to push alexa handlers into an async function.. it has to be used outside the async functions.. there is no way to return a value from an async function? sounds terrible to me.. p.s. i am new to javascript – thealchemist Aug 27 '19 at 20:03
  • Can you add some more context to your question? I don't think we have enough information to handle your question the way you need. It would also be a good idea to read up on javascript, because it seems like you're not at all familiar how asynchronous calls are handled in Javascript. – Michael Aug 27 '19 at 20:10
0

There is no way to return a value outside of a then block or outside of an async function.. thanks a lot everyone for taking your time in responding to my question :) much appreciated..

thealchemist
  • 397
  • 7
  • 24