我正在发出 API 请求,并想通过请求返回的数据向用户提出一个问题。我调用一个函数,该函数执行请求并返回适当的响应:
httpRequest(params).then(function(body) {
console.log(body);
this.emit(':ask', speechOutput, repromptSpeech);
});
this.emit 函数返回一个未处理的承诺拒绝错误。如何等待请求回调被执行,然后发出 :ask 事件?
this
承诺处理程序的内部与this
外部不同,因此我认为未处理的承诺拒绝可能表明这this.emit
不是一个函数。
一个快速的解决方案是使用箭头函数,这可能是您自己的答案中的代码也起作用的原因:
// `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);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句