Nodejs 回调函数
1、阻塞代码
创建文件input.txt, 内容为
hello world in input.txt
创建samp3.js
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
输出:
hello world in input.txt 程序执行结束!
2、非阻塞代码实例
var fs = require("fs");
fs.readFile('input.txt', function(err, data){
if(err)
return console.error(err);
console.log(data.toString());
});
console.log("程序执行结束!");
输出
程序执行结束! hello world in input.txt