promise的簡單實現(xiàn)

        let PENDING = "PENDING";
        let FULFILLED = "FULFILLED";
        let REJECTED = "REJECTED";
        class MyPromise{
            constructor(callback){
                //判斷傳入promise的參數(shù)是不是函數(shù)類型
                if(typeof callback !== "function"){
                    console.error("argument is not a function!")
                    return new Error("argument is not a function!")
                }
                //初始化promise的狀態(tài)
                this.status = PENDING;
                //定義返回值
                this.res;
                //then回調(diào)函數(shù)的隊列
                this._fulfilledQueues = [];
                this._rejectedQueues = [];

                callback(this._resolve.bind(this), this._reject.bind(this))
            }
            //定義傳入callback函數(shù)的resolve函數(shù)(更合理的應該是_resolve和_reject方法定義在constructor里,因為promise實例沒有這兩個方法)
            _resolve(param){
                //加上狀態(tài)的判斷是因為promise的狀態(tài)一旦更改,就不會改變這個特性,為了避免用戶多次調(diào)用resolve/reject函數(shù)造成的狀態(tài)更改
                if(this.status !== PENDING){
                    return;
                }
                //更改promise實例的狀態(tài)為成功resolve
                this.status = FULFILLED;
                this.res = param;
                //狀態(tài)改變,調(diào)用隊列中的函數(shù)
                this._fulfilledQueues.forEach((fn, index) => {
                    fn(this.res)
                })
            }
            _reject(err){
                //加上狀態(tài)的判斷是因為promise的狀態(tài)一旦更改,就不會改變這個特性,為了避免用戶多次調(diào)用resolve/reject函數(shù)造成的狀態(tài)更改
                if(this.status !== PENDING){
                    return;
                }
                this.status = REJECTED;
                this.res = new Error(err)
                //狀態(tài)改變,調(diào)用then中的函數(shù)
                this._rejectedQueues.forEach((fn, index) => {
                    fn(this.res)
                })
            }
            //then方法
            then(onFulfilled, onRejected){
                //從實例中取出promise對象的當前狀態(tài)
                const {status, res} = this;
                switch(status) {
                    case PENDING : //將then方法的函數(shù)加入隊列(狀態(tài)還處在pending狀態(tài),只有在狀態(tài)改變后,才會從隊列中取出函數(shù)進行調(diào)用,現(xiàn)在先進行存儲)
                        this._fulfilledQueues.push(onFulfilled)
                        this._rejectedQueues.push(onRejected)
                        break;
                    case FULFILLED ://執(zhí)行當前then傳入的成功回調(diào)函數(shù)(因為狀態(tài)已經(jīng)確定,所以立馬執(zhí)行函數(shù))
                        onFulfilled(res)
                        break;
                    case REJECTED : //執(zhí)行當前then傳入的失敗回調(diào)函數(shù)(因為狀態(tài)已經(jīng)確定,所以立馬執(zhí)行函數(shù))
                        onRejected(res)
                        break;
                }
                //返回新promise對象,以便可以鏈式調(diào)用then方法
                //注意,不太懂onFulfilledNext, onRejectedNext
                return new MyPromise((onFulfilledNext, onRejectedNext)=>{})
            }
        }
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容