let fs = require('fs');
let man = {};
class Events {
constructor() {
this.stack = [];
}
on(callback) {
this.stack.push(callback);
}
emit() {
this.stack.forEach(callback => callback());
}
}
let events = new Events();
events.on(()=>{
console.log('done');
});
events.on(() => {
if(Object.keys(man).length === 2) {
console.log('finish');
}
})
fs.readFile('./name.txt', 'utf8', function(err, data){
man.name = data;
events.emit();
})
fs.readFile('./age.txt', 'utf8', function(err, data){
man.age = data;
events.emit();
})
网友评论