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

2019-06-16  本文已影响0人  DreamNeverDie
<!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>

上一篇 下一篇

猜你喜欢

热点阅读