还是先以读文件举例:
let fs = require("fs")
//同步读取,返回的是buffer
let read1 = fs.readFileSync("./1.txt")
console.log(read1.toString())
let read2 = fs.readFileSync("./2.txt")
console.log(read2.toString())
let read3 = fs.readFileSync("./3.txt")
console.log(read3.toString())
得到的输出结果永远是1,2,3的顺序
使用异步读取
let fs = require("fs")
//异步读取,顺序有可能有变化
let read4 = fs.readFile("./1.txt",(err,data)=>{
console.log(data.toString());
})
let read5 = fs.readFile("./2.txt",(err,data)=>{
console.log(data.toString());
})
let read6 = fs.readFile("./3.txt",(err,data)=>{
console.log(data.toString());
})
由于数据缓冲区的存在,可能会先读取下方的代码