Javascript异步功能返回承诺而不是实际值

时间:2020-09-03 01:58:21

标签: 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> }吗?

非常感谢您的帮助,在此感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:2)

要获得实际价值,您应该在另一个异步函数中进行等待。

更新代码:

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