首页 > 解决方案 > 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> }

将不胜感激任何帮助,并提前感谢您的时间。

标签: javascript

解决方案


要获得真正的价值,您应该在另一个异步函数中等待。

更新代码:

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());
})();

推荐阅读