美文网首页
Promise-01 三种状态

Promise-01 三种状态

作者: 呆桃冲鸭冲鸭 | 来源:发表于2020-09-10 20:30 被阅读0次

    三种状态:

    1. pending
    2. fulfilled
    3. rejected
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>my</title>
    </head>
    <body>
        <p>test my</p>
    </body>
    <script type="module">
        import MyPromise from './promise.js';
        // 三种状态 1.pending 2.fulfilled 3.rejected
        let myP = new MyPromise((resolve,reject)=>{
            // resolve("suc");
            reject("err");
        });
        console.log(myP);
    
    </script>
    </html>
    
    promise.js:
    export default class MyPromise{
        constructor(handle){
            this.state = "pending";
            this.result = undefined;
    
            // handle(function(val){
            //     // resolve
            //     this.state = "fulfilled";
            //     this.result = val;
            // }.bind(this),function(val){
            //     // rejected
            //     this.state = "rejected";
            //     this.result = val;
            // }.bind(this));
    
            // handle((val)=>{
            //     // resolve
            //     this.state = "fulfilled";
            //     this.result = val;
            // },(val)=>{
            //     // rejected
            //     this.state = "rejected";
            //     this.result = val;
            // });
    
            handle(this._resolve.bind(this),this._reject.bind(this));
        };
        _resolve(val){
            this.state = "fulfilled";
            this.result = val;
        };
        _reject(val){
            this.state = "rejected";
            this.result = val;
        };
    }
    
    打印结果

    相关文章

      网友评论

          本文标题:Promise-01 三种状态

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