美文网首页
2019-06-16 手写简易promise(blue老师)

2019-06-16 手写简易promise(blue老师)

作者: DreamNeverDie | 来源:发表于2019-06-16 19:05 被阅读0次
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <script>
        class Promise2{
          constructor(cb){
            this.end=false;
            this.err=false;
            this.result=null;
            
            cb((...args)=>{
              this.end=true;
              this.err=false;
              this.result=args;
    
              if(this.succ){
                this.succ(...args);
              }
            }, (...args)=>{
              this.end=true;
              this.err=true;
              this.result=args;
    
              if(this.faild){
                this.faild(...args);
              }
            });
          }
    
          then(succ, faild){
            this.succ=succ;
            this.faild=faild;
    
            if(this.end){
              if(this.err){
                faild(...this.result);
              }else{
                succ(...this.result);
              }
            }
          }
        }
        </script>
      </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:2019-06-16 手写简易promise(blue老师)

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