SyntaxError: await is only valid in async function
这个错误的意思是await只能放到async函数内部,言下之意:
- await必须放到函数里
- 函数必须有async修饰符
const myFun = async () => { return new Promise((resolve, reject) => { setTimeout(() => { resolve(1) },1000) }) } const myFun2 = async () => { res1 = await myFun(); console.log(res1); } myFun2(); // 1