【发布时间】:2019-09-27 01:11:28
【问题描述】:
在我的对话流代理中,作为实现,我在代码中有一部分调用了一个 Promise,并且机器人使用来自服务器的一些数据来响应用户。如果成功,一切都会按预期进行。但是,如果出现错误(例如,切断),我想向用户打印一条消息,说明发生了错误,他/她不需要等待永恒的答案。
return new Promise( (resolve,reject) => {
const request = require('request-promise-native');
const options = {
uri: 'https://server.server.com/example',
method: 'POST',
headers: {
'api-token': 'code'
},
body: {
user:'user_name'
},
json: true
};
request(options)
.then(function (body) {
// POST succeeded
console.log("got: "+ body);
agent.add("SUCCESS");
resolve();
})
.catch(function (err) {
// POST failed...
console.log("ERROR!: " + err);
agent.add("I am sorry for the inconvenience but an error happened during your last call.");
agent.add("Please try in a while");
console.log(err.stack);
});
});
失败的 POST 消息后的错误消息打印在日志文件中(因此它进入 .catch 部分)但用户没有收到任何消息,agent.add() 函数在其中不起作用案件,这似乎很奇怪。当 promise 过程失败时,如何向用户打印消息?
【问题讨论】:
标签: node.js dialogflow-es