发布-订阅
                            
                            
                            let fs = require('fs');
// “發布“ -> 中間代理 <- “訂閱“
//先聲明一個函數
function Events(){ this.callbacks = [];this.results = [];
}
//在  Events 的上新建一個 訂閱函數 傳遞一個參數 參數也是一個函數
Events.prototype.on = function(callback){ // 訂閱//將回調push  到  callbacks 數組中,這個數組是中間代理this.callbacks.push(callback);
}
// 新建一個發布函數 
Events.prototype.emit = function(data){ // 發布//將 數據傳遞到 results 數組中this.results.push(data);//遍歷訂閱 數組里面的回調 將結果傳遞過去this.callbacks.forEach(c=>c(this.results));
}
let e = new Events();
e.on(function(arr){if(arr.length === 3){console.log(arr);}
})
fs.readFile('./a.txt','utf8',function(err,data){ // 5s//傳遞數據到發布的函數 
    e.emit(data)
});
fs.readFile('./a.txt','utf8',function(err,data){ // 3s
    e.emit(data)
});
fs.readFile('./c.txt','utf8',function(err,data){ // 3s
    e.emit(data)
});  
                        ?
轉載于:https://www.cnblogs.com/guangzhou11/p/11318393.html
總結
 
                            
                        - 上一篇: CentOS7 安装 mysql8
- 下一篇: 类型保护
