Skip to content
This repository was archived by the owner on May 20, 2020. It is now read-only.

Latest commit

 

History

History
31 lines (28 loc) · 785 Bytes

File metadata and controls

31 lines (28 loc) · 785 Bytes
//简型promise
    function Promise(fn){
        var state = "pending";
        var value = null;
        var callbacks = [];

        this.then = function(onFullfilled){
            return new Promise(function(reslove){
                 if(state === "pending"){
                    callbacks.push(onFullfilled);
                    return this;
            }) 
          
            // }
            // onFullfilled(value);
            // return this;
        }

        function reslove(newValue){
            value = newValue;
            state = "fullfilled";
            setTimeout(function(){
                callbacks.forEach(function(callback){
                    callback(value);
                })
            },0);
        }
        fn(reslove);
    }