(1)events
1. 触发自定义事件
类似jq的trigger函数,用emit去触发另一个事件
const RootEvent = require('events');
class CurEvent extends RootEvent{
}
const DemoEvent = new CurEvent();
DemoEvent.on('printme',function(){
console.log('------------------------------------');
console.log('i am the best');
console.log('------------------------------------');
});
setInterval(()=>{
DemoEvent.emit('printme');
},1000)
(2)fs
1. 创建流读取txt文件
//向txt文件中写入数据,监听finish事件来结束写入
const fs = require('fs');
const ws = fs.createWriteStream('./article.txt');
const continuewrite = setInterval((err)=>{
if(err) throw err;
const num = parseInt(Math.random()*10);
console.log('------------------------------------');
console.log(num);
console.log('------------------------------------');
if(num < 9){
ws.write(num + '');
}else{
clearInterval(continuewrite);
ws.end();
}
},1000);
ws.on('finish',()=>{
console.log('done');
})