为什么在callback之前运行fs.readFilecallback函数下面的代码?
fs.readFile('input.txt', function(err, data) { if (err) { return console.error(err); } console.log(data.toString()); }); console.log("Program Ended");
input.txt
包含string"hello"
。
上面的代码打印:
Program Ended hello
为什么在"hello"
之前打印"program ended"
"hello"
?
这不是一行一行的执行吗?
你的callback函数里面的代码(打印文件内容的那个)不会阻塞执行。 在文件被读取后它被asynchronous执行。