• 手写Promise


    1.声明promise类并且绑定this

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
    
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            executor(this.resolve.bind(this), this.rejectud.bind(this));
        }
        resolve(value) {
            this.status = myPromise.FULFILLED;
            this.value = value;
        }
        rejectud(reason) {
            this.status = myPromise.REJECTED;
            this.value = reason;
        }
    }

    存在问题:从状态变为成功态后还可以二次改变

    2.状态保护和执行者异步捕获

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
     
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            try{
                executor(this.resolve.bind(this), this.rejectud.bind(this));
            }catch(err){
                this.rejectud(err);
            }     
        }
    resolve(value) {
    if(this.status==myPromise.PENDDING){ this.status = myPromise.FULFILLED; this.value = value; } } rejectud(reason) { if(this.status==myPromise.PENDDING){ this.status = myPromise.REJECTED; this.value = reason; } } }

    3.THEN的基础构建

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
        
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            try{
                executor(this.resolve.bind(this), this.rejectud.bind(this));
            }catch(err){
                this.rejectud(err);
            }     
        }
        resolve(value) {
            if(this.status==myPromise.PENDDING){
                this.status = myPromise.FULFILLED;
                 this.value = value;
            }
        }
        rejectud(reason) {
            if(this.status==myPromise.PENDDING){
            this.status = myPromise.REJECTED;
            this.value = reason;
            }
        }
        //可以完成第一次then但是不能链式调用
        then(onFulfilled,onRejected){
            if(typeof onFulfilled !== "function"){
                onFulfilled= ()=>{}
            }
            if(typeof onRejected !== "function"){
                onRejected= ()=>{}
            }
            if(this.status=myPromise.FULFILLED){
                onFulfilled(this.value);
            }
            if(this.status=myPromise.REJECTED){
                onREJECTED(this.value);
            }
        }
    }

    4.实现then的异步操作和异步捕获

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
    
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            try {
                executor(this.resolve.bind(this), this.rejectud.bind(this));
            } catch (err) {
                this.rejectud(err);
            }
        }
        resolve(value) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.FULFILLED;
                this.value = value;
            }
        }
        rejectud(reason) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.REJECTED;
                this.value = reason;
            }
        }
        //可以完成第一次then但是不能链式调用
        then(onFulfilled, onRejected) {
            if (typeof onFulfilled !== "function") {
                onFulfilled = () => { }
            }
            if (typeof onRejected !== "function") {
                onRejected = () => { }
            }
            if (this.status == myPromise.FULFILLED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onFulfilled(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
            if (this.status == myPromise.REJECTED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onRejected(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
        }
    }

    5.promise的pendding状态处理

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
    
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            this.callbacks=[];//定义数组当用户调用定时器时,会将状态存入数组,定时结束后运行
            try {
                executor(this.resolve.bind(this), this.rejectud.bind(this));
            } catch (err) {
                this.rejectud(err);
            }
        }
        resolve(value) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.FULFILLED;
                this.value = value;
                this.callbacks.map(callback=>{
                    callback.onFulfilled(value);
                })
            }
        }
        rejectud(reason) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.REJECTED;
                this.value = reason;
                this.callbacks.map(callback=>{
                    callback.onRejected(reason);
                })
            }
        }
        //可以完成第一次then但是不能链式调用
        then(onFulfilled, onRejected) {
            if (typeof onFulfilled !== "function") {
                onFulfilled = () => { }
            }
            if (typeof onRejected !== "function") {
                onRejected = () => { }
            }
            if(this.status == myPromise.PENDDING){
                this.callbacks.push({
                    onFulfilled, 
                    onRejected
                })
            }
            if (this.status == myPromise.FULFILLED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onFulfilled(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
            if (this.status == myPromise.REJECTED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onRejected(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
        }
    }

    6.pendding的状态异常处理

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
    
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            this.callbacks=[];//定义数组当用户调用定时器时,会将状态存入数组,定时结束后运行
            try {
                executor(this.resolve.bind(this), this.rejectud.bind(this));
            } catch (err) {
                this.rejectud(err);
            }
        }
        resolve(value) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.FULFILLED;
                this.value = value;
                this.callbacks.map(callback=>{
                    callback.onFulfilled(value);
                })
            }
        }
        rejectud(reason) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.REJECTED;
                this.value = reason;
                this.callbacks.map(callback=>{
                    callback.onRejected(reason);
                })
            }
        }
        //可以完成第一次then但是不能链式调用
        then(onFulfilled, onRejected) {
            if (typeof onFulfilled !== "function") {
                onFulfilled = () => { }
            }
            if (typeof onRejected !== "function") {
                onRejected = () => { }
            }
            if(this.status == myPromise.PENDDING){
                this.callbacks.push({
                    onFulfilled: value=>{
                        try {
                            onFulfilled(value)
                            resolve(value)
                        } catch (error) {
                            onRejected(error)
                        }
                    },
                    onRejected: value=>{
                        try {
                            onRejected(value)
                            resolve(value)
                        } catch (error) {
                            onRejected(error)
                        }
                    }
                })
            }
            if (this.status == myPromise.FULFILLED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onFulfilled(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
            if (this.status == myPromise.REJECTED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onRejected(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
        }
    }

    7.pendding的异步任务处理

    class myPromise {
        static PENDDING = "pendding";
        static FULFILLED = "fulfilled";
        static REJECTED = "rejected";
    
        constructor(executor) {
            this.status = myPromise.PENDDING;
            this.value = null;
            this.callbacks=[];//定义数组当用户调用定时器时,会将状态存入数组,定时结束后运行
            try {
                executor(this.resolve.bind(this), this.rejectud.bind(this));
            } catch (err) {
                this.rejectud(err);
            }
        }
        resolve(value) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.FULFILLED;
                this.value = value;
                 //因为这里调用方法是异步的
                setTimeout(() => {
                    this.callbacks.map(callback=>{
                        callback.onFulfilled(value);
                    })
                });
               
            }
        }
        rejectud(reason) {
            if (this.status == myPromise.PENDDING) {
                this.status = myPromise.REJECTED;
                this.value = reason;
                //异步
                setTimeout(() => {
                    this.callbacks.map(callback=>{
                        callback.onRejected(reason);
                    })
                });
            }
        }
        //可以完成第一次then但是不能链式调用
        then(onFulfilled, onRejected) {
            if (typeof onFulfilled !== "function") {
                onFulfilled = () => { }
            }
            if (typeof onRejected !== "function") {
                onRejected = () => { }
            }
            if(this.status == myPromise.PENDDING){
                this.callbacks.push({
                    onFulfilled: value=>{
                        try {
                            onFulfilled(value)
                            resolve(value)
                        } catch (error) {
                            onRejected(error)
                        }
                    },
                    onRejected: value=>{
                        try {
                            onRejected(value)
                            resolve(value)
                        } catch (error) {
                            onRejected(error)
                        }
                    }
                })
            }
            if (this.status == myPromise.FULFILLED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onFulfilled(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
            if (this.status == myPromise.REJECTED) {
                //放到setTimeout中,改为异步操作
                setTimeout(() => {
                    try {
                        onRejected(this.value);
                    } catch (error) {
                        onRejected(error);
                    }
                });
            }
        }
    }

    8.then的链式操作原理分析及实现

     1.then最终返回的是promise

    2.then最终返回的promise的状态默认为resolve态,不会受之前状态的影响

  • 相关阅读:
    LeetCode 图解 | 200 .岛屿数量
    再不跳槽,应届毕业生拿的都比我多了!
    二叉树就是这么简单(面试常考点)
    我和面试官之间关于操作系统的一场对弈!写了很久,看完你就可以盘面试官了...
    非常详细的 Linux C/C++ 学习路线总结!助我拿下腾讯offer
    这款开源神器,让你能在 iPad 上随心所欲写代码!
    不瞒你说,我最近整理了一套面试题(Java岗)
    如何看待第三方百度云 Pandownload 作者被捕?
    【故事】为了避免产品经理和程序猿干架,我用大白话讲清楚了浏览器缓存原理...
    自己拥有一台服务器可以做哪些很酷的事情?搭建私有云网盘!
  • 原文地址:https://www.cnblogs.com/kangxinzhi/p/13825766.html
Copyright © 2020-2023  润新知