问题
I'm using typescript@next
and I want to compile my code to es5
, but each time I'm using async
or await
keywords the compiler errors with that message:
Cannot find name 'await'.
Heres my libs: dom
, es2015
, es2016
, es2017
.
Code example:
let asyncFn = () => {
return new Promise((resolve:Function)=>{resolve(2)})
}
// should log `2`
console.log(await asyncFn())
Such things are possible even with typescript@2.0.x
, I've tried it, but somehow I am unable to compile my code anyway.
回答1:
You need to use your asyncFn inside a function marked as an 'async' function. For example:
async someAsyncCode() {
let asyncFn = () => {
return new Promise((resolve: Function) => { resolve(2); });
}
// should log `2`
console.log(await asyncFn());
}
来源:https://stackoverflow.com/questions/40400806/typescript-cannot-find-name-async-await