【发布时间】:2019-02-26 08:02:34
【问题描述】:
我正在尝试使用异步函数在另一个函数中调用一个函数。它看起来像这样:
const getConnectionsWithEmailsHash = async () => {
const connectionsWithEmails = await parseConnections('./tmp/connections.csv')
console.log(connectionsWithEmails)
return connectionsWithEmails
}
const connectionsWithEmailsHash = getConnectionsWithEmailsHash()
console.log(connectionsWithEmailsHash)
当我在异步函数中使用 console.log() 时,我得到了我期望的哈希值,但是当我 console.log() 调用异步函数的结果时,我得到了未决的承诺。我虽然异步函数的要点是它在调用它时等待承诺被解决,所以我做错了什么?
【问题讨论】:
-
你忘了等待它。你不能像那样跳出异步逻辑
-
我在函数中等待它。我认为 await 只能在异步函数中使用?
-
没错,你不能在异步函数之外使用 await。您也无法返回或访问尚不存在的值。
标签: javascript node.js async-await