node.js - 使用 actions-on-google node v2 请求承诺
问题描述
我有一个类似的问题,例如在 DialogFlow WebHook 中使用异步函数,但是该解决方案,更改 request-promises-native 的 request-promises,对我不起作用,不同之处在于我使用 Actions-on-google lib 和 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;
}
它就像一个魅力。我不明白我错在哪里,我正在返回承诺,直到意图。任何想法?
谢谢!
解决方案
你可以使用 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
})();
推荐阅读
- delphi - Delphi 将文件从 Windows 资源管理器拖放到 TListView 不起作用
- php - 从 Phalcon 模型中获取 PHP 中 DateTime 列的 DateTime 对象
- javascript - 阻止打印预览对话框并直接打印元素
- macos-mojave - (MacOS Mojave 10.14.6) - 为什么 /usr/local 中的“lib”是 Unix 可执行文件而不是文件夹?
- javascript - Slidetoggle() 功能无法正常工作
- java - 这个 Activity 已经有一个由窗口装饰提供的操作栏。更新 android studio 后
- r - 如何删除 2 组的一些 NA
- python - Python 多处理 - Pool.map 的结果为无
- java - 在输入中使用空格时 Java 扫描器输入不匹配
- android - 从使用导航组件的片段返回时会触发哪些方法?