alexa-js / alexa-app-server

An Alexa app server for alexa-app.
MIT License
401 stars 116 forks source link

Asynchronous Functions not working #107

Closed kingandrewsolomon closed 6 years ago

kingandrewsolomon commented 6 years ago

I tried to use promises and async / await to run my intents but they don't seem to work, so I suspected it was something with the server running it so I created a test event to double check with a setTimeout call and that doesn't seem to work either. Any thoughts or ideas are welcome!

dblock commented 6 years ago

Your function doesn't return a promise? Show us some code.

kingandrewsolomon commented 6 years ago

app.intent('TestIntent', { "utterances": ["test"] }, function(req, res) { setTimeout(() => { res.say('hello'); }, 500); });

dblock commented 6 years ago

Your code doesn't return a promise, and it needs to. This SO explains quite well how to use promises and setTimeout, I think: https://stackoverflow.com/questions/39538473/using-settimeout-on-promise-chain

I'll close this since the library behaves as expected, but feel free to ask more questions on the thread.

kingandrewsolomon commented 6 years ago

Okay so I fixed the stupid mistake (😅) but it still doesn't work:

app.intent('TestIntent', {
    "utterances": ["test"]`
}, function(req, res) {
    delay().then((data) => {
        console.log("res");
        res.say("data");
    });
    res.say("asdf");
});

function delay() {
    return new Promise((resolve, reject) => {
        setTimeout(resolve("hello"), 1000);
    });
}

the asdf gets sent through but data does not. It does get triggered, hello would come through but it never gets seen in the response

dblock commented 6 years ago

In Javascript when return is omitted, the function does not return a value. So you need to return delay()...

kingandrewsolomon commented 6 years ago

Oh my gosh I'm so stupid. Thank you for your time. I'm so sorry I completely missed all of that. I'm actually embarrassed about this issue, is there a way to delete it? 😅

dblock commented 6 years ago

Please don't be embarrassed and happy to help! There're no stupid questions and you're not stupid. JavaScript is ;) I've been equally confused about promises in my past life. Lets leave this here for the next person with a similar problem.