tsc 不允许我使用 await 关键字进行编译,即使我使用的函数是异步的
tsc won't let me compile using the await keyword, even thought the function I'm using is async
当我尝试编译时:
const result = await asyncFunction();
我收到“'await' 表达式只允许使用异步函数”错误,即使我使用的函数使用的是 async 关键字。我在使用多个为其他人工作的不同异步函数时不断收到此错误,所以我知道这是我做错了什么。
我正在使用 typescript 3.2.2,并编译到节点 10.15.0,但也尝试编译到 11.6
The await
operator is used to wait for a Promise
. It can only be used inside an async function
.
此代码有效:
async function test() {
const result = await asyncFunction();
}
当我尝试编译时:
const result = await asyncFunction();
我收到“'await' 表达式只允许使用异步函数”错误,即使我使用的函数使用的是 async 关键字。我在使用多个为其他人工作的不同异步函数时不断收到此错误,所以我知道这是我做错了什么。
我正在使用 typescript 3.2.2,并编译到节点 10.15.0,但也尝试编译到 11.6
The
await
operator is used to wait for aPromise
. It can only be used inside anasync function
.
此代码有效:
async function test() {
const result = await asyncFunction();
}