【问题标题】:How to wait for the promise to get resolved如何等待承诺得到解决
【发布时间】:2020-10-17 00:28:57
【问题描述】:

我正在尝试在 nodejs 的异步函数中编写一个函数。这是我的示例脚本

module.exports = async function (context, req) {
   var mydata = (async () => {
     return "output needed"
   }) ()
  
   console.log(mydata)
}

预期输出为:output needed

我得到的是:promise{ <pending> }

有什么想法等到承诺兑现?

【问题讨论】:

  • 一切看起来都是同步的。删除同步

标签: node.js promise async-await es6-promise azure-function-app


【解决方案1】:

你需要等待mydata返回的Promise

async function test(context, req) {
   var mydata = (async () => {
     return "output needed"
   }) ()
  
   console.log(await mydata)
}

test()

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 2019-09-23
    • 1970-01-01
    • 2019-04-29
    • 2017-05-25
    • 2022-01-17
    • 2018-04-02
    • 1970-01-01
    • 2014-03-12
    相关资源
    最近更新 更多