异步HTTP请求完成后,AWS Lambda Node.js执行this.emit [英] AWS Lambda Node.js execute this.emit after async HTTP request completes

查看:76
本文介绍了异步HTTP请求完成后,AWS Lambda Node.js执行this.emit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提出一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆