当前位置:网站首页>NodeJS(二)同步读取文件和异步读取文件
NodeJS(二)同步读取文件和异步读取文件
2022-04-23 06:27:00 【老少年】
var fs = require("fs");
var data = fs.readFileSync('test.txt');
console.log(data.toString());
console.log("Program Ended 001");
fs.readFile('test.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended 002");
引入fs模块,同步读取函数readFileSync,异步读取函数readFile。
一般情况下NodeJS的每一个API都是异步的,作为一个单独的线程,它使用异步函数调用来维护并发。Node使用观察者模式。Node线程保持一个事件循环,每当任何任务完成后得到结果,它触发通知事件侦听函数来执行相应的事件。
版权声明
本文为[老少年]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laoshaonian/article/details/101543720
边栏推荐
猜你喜欢
'NPM' is not an internal or external command, nor is it a runnable program or batch file
Custom time format (yyyy-mm-dd HH: mm: SS week x)
反思 | Android 音视频缓存机制的系统性设计
Date object (JS built-in object)
ABAP 实现发布RESTful服务供外部调用示例
Super classic & Programming Guide (red and blue book) - Reading Notes
Design optimization of MySQL database
Redis connection error err auth < password > called without any password configured for the default user
js之节点操作,为什么要学习节点操作
SAP pi / PO rfc2soap publishes RFC interface as WS example
随机推荐
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
3.排序语句
‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
【自我激励系列】到底是什么真正阻碍了你?
js之自定义属性以及H5中如何判断自定义属性
js之DOM学习三种创建元素的方式
【TED系列】一个习惯是如何改变我的一生
1. View databases and tables
SAP PI/PO rfc2Soap 发布rfc接口为ws示例
Ogldev reading notes
Understanding of STL container
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
对js中argumens的简单理解
7. sub query
RGB颜色转HEX进制与单位换算
2022.3.14 阿里笔试
Date对象(js内置对象)
简单理解==和equals,String为什么可以不用new
Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
10. Update operation