You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
letpubSub={subs: [],subscribe(key,fn){//订阅if(!this.subs[key]){this.subs[key]=[];}this.subs[key].push(fn);},publish(...arg){//发布letargs=arg;letkey=args.shift();letfns=this.subs[key];if(!fns||fns.length<=0)return;for(leti=0,len=fns.length;i<len;i++){fns[i](args);}},unSubscribe(key){deletethis.subs[key]}}//测试pubSub.subscribe('name',name=>{console.log(`your name is ${name}`);})pubSub.subscribe('gender',gender=>{console.log(`your name is ${gender}`);})pubSub.publish('name','leaf333');// your name is leaf333pubSub.publish('gender','18');// your gender is 18
回调函数
多层嵌套,形成“回调地狱”,代码可读性和可维护性差;而且如下面读取文件来说,如果其中某个文件读取失败,还要做失败处理,复杂程度可想而知...
事件监听
发布订阅模式
Promise
链式调用规避了大量的嵌套,复合线性思维;
async + await
参考
The text was updated successfully, but these errors were encountered: