I'm working on an Alexa skill in Node that runs in AWS Lambda, and having trouble getting a callback to execute when I emit an event. The Alexa-skills-kit for Node.JS README demonstrates passing a callback function to an event handler, and recommends using the arrow function to preserve context:
this.emit('JustRight', () => {
this.emit(':ask', guessNum.toString() + 'is correct! Would you like to play a new game?',
'Say yes to start a new game, or no to end the game.');
});
I'm trying to do the same, but find that my callback never appears to be executed. I thought this was because AWS Lambda is limited to Node 4.3.2, and the arrow function is not available, so I tried passing this context back to the callback the old fashioned way:
In New Session handler:
if(!account_id) {
console.log('Access token:' + accessToken);
this.emit('getAccount', accessToken, function (retrieved_id) {
console.log('account id in callback: ' + retrieved_id);
this.emit('welcome');
});
}
In event handler:
accountHandler = {
'getAccount': function (accessToken, cb) {
console.log('fetching account id');
var client = thirdparty.getClient(accessToken);
var r = client.getAccountForToken(client);
r.then(function (data) {
console.log('got it:' + data);
this.attributes['account_id'] = data;
cb.call(this, data);
}).catch(function (err) {
this.emit('handleApiError', err);
});
},
}
I can see that I'm successfully retrieving the account ID, in the logs, but Lambda is executing without error and without invoking my callback function. I'm trying to figure out if there is a problem with invoking the callback within the Promise 'then' function, or if something else is going on.