JS异步操作async修饰符

async 函数是使用async关键字声明的函数。 async 函数是AsyncFunction构造函数的实例, 并且其中允许使用await关键字。asyncawait关键字让我们可以用一种更简洁的方式写出基于Promise的异步行为,而无需刻意地链式调用promise

import thenFs from "then-fs";

// 用async与await修饰后,r1则直接表示文件内容
// async方法中,第一个await之前的代码会同步执行,await之后的代码会异步执行
 async function getAllFile(){
    const r1=await thenFs.readFile('./file/1.txt','utf8')

    console.log(r1);
    const r2=await thenFs.readFile('./file/2.txt','utf8')
    console.log(r2);
}
getAllFile();
posted @ 2022-07-03 12:15  小国际  阅读(303)  评论(0)    收藏  举报