我有一个类似的问题,例如Use Async Functions in DialogFlow WebHook,但是该解决方案(将请求-承诺更改为请求-承诺-本机)不适用于我,区别在于我使用的是Google行动库用ActionsSDK代替DialogFlow之一,这是我的代码:
function call() {
var options = {
url: "https://google.es"
};
return request(options)
.then((res) => {
console.log("Success", res);
Promise.resolve();
})
.catch((err) => {
console.log("Error", err);
Promise.resolve();
});
}
const handleAction = (conv) => {
call()
.then(() => {
console.log("Going to ASK");
conv.ask('Hi, how is it going?');
return Promise.resolve();
})
.catch(error => {
console.log("Ask ERROR");
conv.ask('Hi, how is it going?');
return Promise.resolve();
});
}
app.intent('actions.intent.MAIN', (conv) => {
handleAction(conv);
});
如果我更改了该函数的调用函数:
function call() {
let prom = new Promise((resolve,reject) =>{
resolve();
});
return prom;
}
它就像一种魅力。我不明白我在哪里错了,我一直在回诺诺言直到意图。有想法吗?
谢谢!
答案 0 :(得分:0)
您可以使用async / await解决此问题。它看起来像这样。可能会对您有帮助。
(async () => {
async function call() {
var options = {
url: "https://google.es"
};
return await new Promise((resolve, reject) => {
request(options)
.then((res) => {
resolve(res);
})
.catch((err) => {
console.log("Error", err);
reject(err)
});
}
}
const handleAction = await call(); //you will get result on handle Action varaible
})();