• promise 实现


    1实现异步调用
    const PENDDING = 'pendding',
        RESOLVED = 'resolved',
        REJECTED = 'rejected'
    	
        class myPromise {
            constructor(handler){
                this.status = PENDDING
                this.value = undefined
                this.resolveCallback = []
                this.rejectCallback = []
                let resolve = (value)=>{
                    if(this.status === PENDDING){
                        this.status = RESOLVED
                        this.value = value
                        this.resolveCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                let reject = (error)=>{
                    if(this.status === PENDDING){
                        this.status = REJECTED
                        this.reason = error
                        this.rejectCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                try{
                    handler(resolve,reject)
                }catch{
                    reject(error)
                }
            }
            then(onResolve=val=>val,onReject){
                if(this.status === PENDDING){
                    this.resolveCallback.push(()=>onResolve(this.value))
                }
                if(this.status === RESOLVED){
                    onResolve(this.value)
                }
                if(this.status === REJECTED){
                    onReject(this.reason)
                }
            }
        }
    
        new myPromise((resolve)=>{
            setTimeout(()=>{
                resolve(123)
            },2000)
        }).then((res)=>{
            console.log(res)
        })
    

    2.实现 链式调用并返回普通值 主要是promiseResultProduce这个函数

     const PENDDING = 'pendding',
        RESOLVED = 'resolved',
        REJECTED = 'rejected'
        class myPromise {
            constructor(handler){
                this.status = PENDDING
                this.value = undefined
                this.resolveCallback = []
                this.rejectCallback = []
                let resolve = (value)=>{
                    if(this.status === PENDDING){
                        this.status = RESOLVED
                        this.value = value
                        this.resolveCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                let reject = (error)=>{
                    if(this.status === PENDDING){
                        this.status = REJECTED
                        this.reason = error
                        this.rejectCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                try{
                    handler(resolve,reject)
                }catch{
                    reject(error)
                }
            }
            promiseResultProduce(promise2,val,resolve,reject){
                if(val instanceof myPromise){
    
                }else if((typeof val === 'object' || typeof val === 'function')){
                    if(typeof val.then === 'function'){
    
                    }else{
                        resolve(val)
                    }
                }else{
                    resolve(val)
                }
            }
            then(onResolve=val=>val,onReject){
                if(this.status === PENDDING){
                    const promise2 = new myPromise((resolve,reject)=>{
                        this.resolveCallback.push(()=>{
                            let val = onResolve(this.value)
                            setTimeout(()=>{
                                this.promiseResultProduce(promise2,val,resolve,reject)
                            },0)
                        })
                    })
                    return promise2
                }
                if(this.status === RESOLVED){
                    const promise2 = new myPromise((resolve,reject)=>{
                        let val = onResolve(this.value)
                        setTimeout(()=>{
                            this.promiseResultProduce(promise2,val,resolve,reject)
                        },0)
                    })
                    return promise2
                }
                if(this.status === REJECTED){
                    onReject(this.reason)
                }
            }
        }
        
        new myPromise((resolve)=>{
            setTimeout(()=>{
                resolve(123)
            },2000)
        }).then((res)=>res)
        .then((res)=>{
            console.log(res)
            return res
        }).then((res)=>{
            console.log(res)
        })
    

    3 实现 支持promise对象

        const PENDDING = 'pendding',
        RESOLVED = 'resolved',
        REJECTED = 'rejected'
        class myPromise {
            constructor(handler){
                this.status = PENDDING
                this.value = undefined
                this.resolveCallback = []
                this.rejectCallback = []
                let resolve = (value)=>{
                    if(this.status === PENDDING){
                        this.status = RESOLVED
                        this.value = value
                        this.resolveCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                let reject = (error)=>{
                    if(this.status === PENDDING){
                        this.status = REJECTED
                        this.reason = error
                        this.rejectCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                try{
                    handler(resolve,reject)
                }catch{
                    reject(error)
                }
            }
            promiseResultProduce(promise2,val,resolve,reject){
                if(val instanceof myPromise){
                    if(val.status === PENDDING){
                        console.log(222)
                        val.then(y=>{
                            console.log(y)
                            this.promiseResultProduce(promise2,y,resolve,reject)
                        },reject)
                    }else{
                        /*注意此时val是promise自然有this.value,this.status,this.reason属性*/
                        val.status === RESOLVED && resolve(val.value)
                        val.status === REJECTED && reject(val.reason)
                    }
                } else if((typeof val === 'object' || typeof val === 'function')){
    
                } else{
                    console.log(1111)
                    resolve(val)
                }
            }
            then(onResolve=val=>val,onReject){
                if(this.status === PENDDING){
                    const promise2 = new myPromise((resolve,reject)=>{
                        this.resolveCallback.push(()=>{
                            let val = onResolve(this.value)
                            setTimeout(()=>{
                                this.promiseResultProduce(promise2,val,resolve,reject)
                            },0)
                        })
                    })
                    return promise2
                }
                if(this.status === RESOLVED){
                    const promise2 = new myPromise((resolve,reject)=>{
                        let val = onResolve(this.value)
                        this.resolveCallback.push(()=>{
                            setTimeout(()=>{
                                this.promiseResultProduce(promise2,val,resolve,reject)
                            },0)
                        })
                    })
                    return promise2
                }
                if(this.status === REJECTED){
                    onReject(this.reason)
                }
            }
        }
    
        new myPromise((resolve)=>{
            setTimeout(()=>{
                resolve(123)
            },2000)
        })
        .then((res)=>{
            console.log(res)
            return new myPromise((resolve,reject)=>{
                setTimeout(()=>{
                    resolve(666)
                },2000)
            })
        })
        .then((res)=>{
            console.log(res)
        })
    

    4支持thenable对象

        const PENDDING = 'pendding',
        RESOLVED = 'resolved',
        REJECTED = 'rejected'
        class myPromise {
            constructor(handler){
                this.status = PENDDING
                this.value = undefined
                this.resolveCallback = []
                this.rejectCallback = []
                let resolve = (value)=>{
                    if(this.status === PENDDING){
                        this.status = RESOLVED
                        this.value = value
                        this.resolveCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                let reject = (error)=>{
                    if(this.status === PENDDING){
                        this.status = REJECTED
                        this.reason = error
                        this.rejectCallback.forEach((cb)=>{
                            cb()
                        })
                    }
                }
                try{
                    handler(resolve,reject)
                }catch{
                    reject(error)
                }
            }
            promiseResultProduce(promise2,val,resolve,reject){
                if(val instanceof myPromise){
                    if(val.status === PENDDING){
                        val.then(y=>{
                            this.promiseResultProduce(promise2,y,resolve,reject)
                        },reject)
                    }else{
                        /*注意此时val是promise自然有this.value,this.status,this.reason属性*/
                        val.status === RESOLVED && resolve(val.value)
                        val.status === REJECTED && reject(val.reason)
                    }
                } else if((typeof val === 'object' || typeof val === 'function')){
                    if(typeof val.then === 'function'){
                        val.then(y=>{
                            this.promiseResultProduce(promise2,y,resolve,reject)
                        },reject)
                    }else{
                        resolve(val)
                    }
                } else{
                    resolve(val)
                }
            }
            then(onResolve=val=>val,onReject){
                if(this.status === PENDDING){
                    const promise2 = new myPromise((resolve,reject)=>{
                        this.resolveCallback.push(()=>{
                            let val = onResolve(this.value)
                            setTimeout(()=>{
                                this.promiseResultProduce(promise2,val,resolve,reject)
                            },0)
                        })
                    })
                    return promise2
                }
                if(this.status === RESOLVED){
                    const promise2 = new myPromise((resolve,reject)=>{
                        let val = onResolve(this.value)
                        this.resolveCallback.push(()=>{
                            setTimeout(()=>{
                                this.promiseResultProduce(promise2,val,resolve,reject)
                            },0)
                        })
                    })
                    return promise2
                }
                if(this.status === REJECTED){
                    onReject(this.reason)
                }
            }
        }
        // 支持thenable对象
        // let result = {
        //     then(r){
        //         r(444)
        //     }
        // }
        // result.then(y=>{
        //     console.log(y)
        // })
    
        /*穿透的原理*/
        // new myPromise((resolve)=>{
        //     setTimeout(()=>{
        //         resolve(123)
        //     },2000)
        // }).then((res)=>res).then((res)=>{
        //     console.log(res)
        // })
    	
    	Promise.allSettled = function(param){
    		let result = []
    		let count = 0
    		let ln = param.length
    		return new Promise((resolve,reject)=>{
    			for(let task of param){
    				Promise.resolve(task).then((res)=>{
    					result[count] = {value:res,status:'fulfilled'};
    					++count === ln && resolve(result)
    				}).catch((err)=>{
    					result[count] = {reason:err,status:'rejected'};
    					++count === ln && resolve(result)
    				})
    			}
    		})
    	}
    	const test2 = 'test2'
    	const test3 = ()=>{
    		return new Promise((resolve,reject)=>{
    			setTimeout(()=>{
    				reject('err')
    			},1000)
    		})
    	}
    	Promise.allSettled([test2,test3()]).then((res)=>{
    		console.log(res)
    	})
    
  • 相关阅读:
    几道php基础面试题
    【转载】VMware下LINUX的虚拟机增加磁盘空间
    【转载】给VM虚拟机增加硬盘容量
    虚拟机扩大硬盘的方法
    【转载】Linux i386+源码中常见宏标识tag的定义
    【转载】Linux下编辑生成.mo文件
    【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误
    【转载】Ubuntu下SVN安装和配置
    【转载】关于shell中的basename
    tar的-t参数使用
  • 原文地址:https://www.cnblogs.com/MDGE/p/14839460.html
Copyright © 2020-2023  润新知