我正在发出API请求,并希望向用户询问有关请求返回的数据的问题。我调用一个函数,该函数执行请求并返回相应的响应:
httpRequest(params).then(function(body) {
console.log(body);
this.emit(':ask', speechOutput, repromptSpeech);
});
this.emit函数返回未处理的promise promise rejection错误。如何等待请求回调执行,然后发出:ask event?
答案 0 :(得分:2)
承诺处理程序中的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);
});
不是' ta功能。
快速解决方案是使用arrow function,这可能也是您自己的答案中的代码也适用的原因:
# include <boost/preprocessor/facilities/empty.hpp>
# include <boost/preprocessor/list/at.hpp>
# include <boost/preprocessor/list/for_each_product.hpp>
# include <boost/preprocessor/tuple/elem.hpp>
# include <boost/preprocessor/tuple/to_list.hpp>
#define DIM BOOST_PP_TUPLE_TO_LIST(2,(2,3))
#define DEG BOOST_PP_TUPLE_TO_LIST(1,(typeA))
#define INSTANTIATE(R, L) \
template void MyClass<BOOST_PP_TUPLE_ELEM(2, 0, L), \
BOOST_PP_TUPLE_ELEM(2, 1, L)>::some_method() const;
BOOST_PP_LIST_FOR_EACH_PRODUCT(INSTANTIATE, 2, (DIM, DEG))
答案 1 :(得分:0)
我最终使用请求库解决了这个问题:
function getEntries() {
return request.get('https://wezift.com/parent-portal/api/entries.json');
}
getEntries().then(
(response) => {
console.log(response);
this.emit(':ask', 'hi', 'hi again');
},
(error) => {
console.error('uh-oh! ' + error);
}
);