Vue - async、await 的使用注意事项
如果在 function 中使用了 await ,则 function 前面要加上 async 修饰
async function getAllFiles(){ const r1 = await thenFs.readFile('./files/1.txt','utf8') console.log(r1) const r2 = await thenFs.readFile('./files/2.txt','utf8') console.log(r2) const r3 = await thenFs.readFile('./files/3.txt','utf8') console.log(r3) } getAllFiles()
如果去掉了 async 、await ,输出的结果会是一个对象
function getAllFile(){ const r1 = thenFs.readFile('./files/1.txt','utf8') console.log(r1) } getAllFile()
在 async 方法中,第一个 await 前的代码会同步执行, await 之后的代码会异步执行
console.log('A')
async function getAllFiles(){
console.log('B')
const r1 = await thenFs.readFile('./files/1.txt','utf8')
console.log(r1)
const r2 = await thenFs.readFile('./files/2.txt','utf8')
console.log(r2)
const r3 = await thenFs.readFile('./files/3.txt','utf8')
console.log(r3)
console.log('D')
}
getAllFiles()
console.log('C')
在上面的这段代码执行结果是,A 、B、C、111、222、333、D
浙公网安备 33010602011771号