javascript - Javascript异步函数返回承诺而不是实际值
问题描述
我目前有一个异步函数,但它只返回Promise { <pending> }
而不是实际的返回值。
我的功能如下:
async function testFunction() {
try {
var resp = await deepai.callStandardApi("sentiment-analysis", {
text: "This is a test.",
});
return resp.output;
}
catch(e) {
console.log(e);
}
}
console.log(testFunction())
有谁知道我如何返回实际内容而不是Promise { <pending> }
?
将不胜感激任何帮助,并提前感谢您的时间。
解决方案
要获得真正的价值,您应该在另一个异步函数中等待。
更新代码:
async function testFunction() {
try {
var resp = await deepai.callStandardApi("sentiment-analysis", {
text: "This is a test.",
});
return resp.output;
}
catch(e) {
console.log(e);
}
}
(async function() {
console.log(await testFunction());
})();
推荐阅读
- c# - 将datagridview单元格值设置为行c#的索引号
- python - 使用 BeautifulSoup 处理 pre 标签中的尖括号
- ios - iPhone蓝牙配对无需确认密码
- android - 如何在数据绑定的片段中使用onClick?
- android - Unity 中的画布在实际手机上使用略有偏差
- java-8 - 如何通过避免手动睡眠来对 CompletableFuture.thenAccept() 进行单元测试
- python - 会话夹具不被调用和全局变量共享
- c# - 为什么我的贴花投影仪同时撞击墙壁和地板时生成错误?
- excel - 查找行中的最后一个条目,然后返回该列顶行中单元格的值
- scala - 如何使用 Spark 2.2 使用 TestHiveContext