zoukankan      html  css  js  c++  java
  • 模拟原生的promise

    class Promise {
        static resolve(data) {
            if (data instanceof Promise) {
                return data
            }
            return new Promise((resolve) => {
                resolve(data)
            })
        }
    
        static reject(data) {
            if (data instanceof Promise) {
                return data
            }
            return new Promise((resolve, reject) => {
                reject(data)
            })
        }
    
        static all(promiseObjs) {
            return new Promise((resolve, reject) => {
                let array = []
                let count = 0
                for (const promiseObj of promiseObjs) {
                    promiseObj.then(value => {
                        array.push(value)
                        count++
                        if (count === promiseObjs.length){
                            resolve(array)
                        }
                    }).catch(reason => {
                        reject(reason)
                    })
                }
            })
        }
    
        static race(promiseObjs){
            return new Promise((resolve, reject) => {
                for (const promiseObj of promiseObjs) {
                    promiseObj.then(value => {
                        resolve(value)
                    }).catch(reason => {
                        reject(reason)
                    })
                }
            })
        }
    
        resolve(data) {
            if (this.PromiseState !== 'pending') return
            this.PromiseState = 'fulfilled'
            this.PromiseResult = data
            // 执行保存的回调
            const fulfills = this.callbacks.fulfilled
            const length = fulfills.length
    
            for (let i = 0; i < length; i++) {
                setTimeout(()=> {
                    this.executeCallback(fulfills.shift(), [this.PromiseResult], 'fulfilled')
                })
            }
        }
    
        reject(data) {
            if (this.PromiseState !== 'pending') return
            this.PromiseState = 'rejected'
            this.PromiseResult = data
            // 执行保存的回调
            const rejects = this.callbacks.rejected
            const length = rejects.length
            for (let i = 0; i < length; i++) {
                setTimeout(()=> {
                    this.executeCallback(rejects.shift(), [this.PromiseResult], 'rejected')
                })
            }
        }
    
        constructor(executor) {
            if (typeof (executor) !== 'function') {
                throw TypeError(`Promise resolver ${executor} is not a function`)
            }
            this.PromiseState = 'pending'
            this.PromiseResult = undefined
    
            // 保存异步修改状态时的回调函数
            this.callbacks = {fulfilled: [], rejected: []}
            // 绑定函数在外部执行时的this指向
            let resolve = this.resolve.bind(this)
            let reject = this.reject.bind(this)
            this.executeCallback(executor, [resolve, reject], 'pending')
        }
    
        then(onResolved, onRejected) {
            if (this.PromiseState === 'fulfilled') {
                setTimeout(()=>{
                    this.executeCallback(onResolved, [this.PromiseResult], this.PromiseState)
                })
    
            }
            if (this.PromiseState === 'rejected') {
                setTimeout(()=>{
                    this.executeCallback(onRejected, [this.PromiseResult], this.PromiseState)
                })
            }
            if (this.PromiseState === 'pending') {
                this.callbacks.fulfilled.push(onResolved)
                this.callbacks.rejected.push(onRejected)
            }
            return this
        }
    
        catch(onRejected) {
            if (this.PromiseState === 'pending') {
                this.callbacks.rejected.push(onRejected)
            }
            if (this.PromiseState === 'rejected') {
                //模拟真Promise的异步执行
                setTimeout(()=>{
                    this.executeCallback(onRejected, [this.PromiseResult], this.PromiseState)
                })
            }
            return this
        }
    
        executeCallback(func, arg, state) {
            if (typeof (func) !== 'function') {
                return
            }
    
            if (state === 'pending') {
                try {
                    func(...arg)
                } catch (e) {
                    this.reject(e)
                }
            }
            if (state === 'fulfilled' || state === 'rejected') {
                try {
                    const rest = func(...arg)
                    // 如果返回的是一个promise则让this里面的属性改为新的promise对象
                    // 如果返回的是非promise对象则直接执行resolve
                    if (rest instanceof Promise) {
                        this.PromiseState = rest.PromiseState
                        this.PromiseResult = rest.PromiseResult
                        this.callbacks = rest.callbacks
                    } else {
                        this.PromiseResult = rest
                    }
                } catch (e) {
                    // 先改状态为pending才能执行reject,即:强制设置为rejected
                    this.PromiseState = 'pending'
                    this.reject(e)
                }
            }
        }
    }
    
    
    (function init(window) {
        window.Promise = Promise
    }(window))
    
    
  • 相关阅读:
    Python随心记---数据类型
    Pycharm报错解决:error:please select a valid Python interpreter 及一些基本设置
    Python随心记--用户登录锁定
    Python随心记-continue 和 break的区别
    Python随心记
    PHP随笔-判断访问者的设备
    JFinal架构简介
    com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect。ecplise连接sqlserver失败,解决方案。亲测有效!!!
    什么是maven,jar包的查找过程?
    SVN:项目管理工具
  • 原文地址:https://www.cnblogs.com/xiasir/p/15112380.html
Copyright © 2011-2022 走看看