理解发布/订阅
异步编程方法
1.回调函数
2.事件发布/订阅
3.Promise
4.generator函数
5.async函数
//回调地狱
function ajax(url, callback){
//实现省略
}
ajax('./test1.json',function(data){
console.log(data);
ajax('./test2.json',function(data){
console.log(data);
ajax('./test3.json',function(data){
console.log(data);
});
});
//发布/订阅
const pbb = new PubSub();
ajax("./test1.json",function(data){
pbb.publish("test1Success",data);
});
pbb.subscribe("test1Success",function(data){
console.log(data);
ajax("./test2.json",function(data){
pbb.publish("test2Success",data);
});
});
pbb.subscribe("test2Success",function(data){
console.log(data);
ajax("./test3.json",function(data){
pbb.publish("test3Success",data);
});
});
pbb.subscribe("test2Success",function(data){
console.log(data);
});
实现发布/订阅
class PubSub{
constructor(){
this.events = {};
}
publish(eventName,data){
if(this.events[eventsName]){
this.events[eventName].forEach(cb => {
cb.apply(this.data)
});
}
}
subscribe(eventName, callback){
if(this.events[eventName]){
this.events[eventName].push(callback);
}else{
this.events[eventName] = [callback];
}
}
unSubcribe(eventName,callback){
if(this.events[eventName]){
this.events[eventName] = this.events[eventName].filter(
cb => cb !== callback
);
}
}
}
Node.js的发布/订阅
});