请问readfile里面的代码是等console.log('hello')执行完再执行还是和他同步执行的?
答:同步执行,hello,运行完在到readfile
要让hello在之前输出,写在readfile回调里面,hello world下面。
var fs = require('fs')
fs.writeFile('./test.txt','hello world',function (err) {
if (err) {
return 0
}
fs.readFile('./test.txt','utf-8', function (err,data) {
console.log(data) /* 输出hello world */
console.log('hello') /* 输出hello */
})
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…