美文网首页
发布订阅模式Pub/Sub

发布订阅模式Pub/Sub

作者: TerdShow | 来源:发表于2019-08-06 11:24 被阅读0次
    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();
    })
    

    相关文章

      网友评论

          本文标题:发布订阅模式Pub/Sub

          本文链接:https://www.haomeiwen.com/subject/yjridctx.html