简体   繁体   English

Javascript异步函数返回承诺而不是实际值

[英]Javascript Async function returns promise and not actual value

I currently have an async function but it only returns Promise { <pending> } and not the actual return value.我目前有一个异步函数,但它只返回Promise { <pending> }而不是实际的返回值。

My function is as follows:我的功能如下:

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

Does anyone know how I can return the actual content and not Promise { <pending> } ?有谁知道我如何返回实际内容而不是Promise { <pending> }

Would appreciate any help at all and thank you in advanced for your time.非常感谢任何帮助,并提前感谢您的时间。

To get the real value, you should do await in another async function.为了获得真正的价值,你应该在另一个异步函数中等待。

Updated Code:更新代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM