异步HTTP请求完成后,AWS Lambda Node.js执行this.emit [英] AWS Lambda Node.js execute this.emit after async HTTP request completes
问题描述
我正在提出一个API请求,并想问用户一个关于从请求返回的数据的问题.我调用一个函数,该函数执行请求并返回适当的响应:
I am making an API request and would like to ask the user a question with the data returned from the request. I make a call to a function, which executes the request and returns the appropriate response:
httpRequest(params).then(function(body) {
console.log(body);
this.emit(':ask', speechOutput, repromptSpeech);
});
this.emit函数返回未处理的promise拒绝错误.如何等待请求回调执行,然后发出:ask事件?
The this.emit function returns an unhandled promise rejection error. How can I wait for the request callback to be executed and then issue the :ask event?
推荐答案
promise处理程序中的this
与外部的this
不同,所以我认为未处理的Promise拒绝可能表示this.emit
不是函数.
The this
inside the promise handler isn't the same as this
outside of it, so I think the unhandled promise rejection might have stated that this.emit
isn't a function.
一种快速的解决方案是使用箭头函数,这可能就是为什么您自己的答案中的代码也能起作用的原因:
A quick solution would be to use an arrow function, which is probably why the code in your own answer works too:
// `this` here...
httpRequest(params).then(body => {
console.log(body);
this.emit(':ask', speechOutput, repromptSpeech); // ...is the same as `this` here
}).catch(error => {
console.error('uh-oh!', error);
});
这篇关于异步HTTP请求完成后,AWS Lambda Node.js执行this.emit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!