zoukankan      html  css  js  c++  java
  • ES6之promise原理

    我在这里介绍了promise的原理: https://juejin.im/post/5cc54877f265da03b8585902

    我在这里 仅仅张贴 我自己实现的简易promise——DiPromise(自己起的名字,嘿嘿嘿~~)

    PS: 这个可以在控制台执行

    let count = 0
    class DiPromise {
        constructor(executor) {
            //  自己维护的状态
            this.state = 'PENDING'
            // FULFILLED or REJECTED 后的结果
            this.value = ''
            // 存储success, reject的回调
            this.handlers = []
            // 绑定this
            this.handle = this.handle.bind(this)
            this.onResolve = this.onResolve.bind(this)
            this.onReject = this.onReject.bind(this)
            this.then = this.then.bind(this)
            this.done = this.done.bind(this)
            // 这个只是调试用
            this.name = count + 1
            count += 1
            // 在new的时候,立即执行executor,并给executor传入两个函数作为其参数
            executor(this.onResolve, this.onReject)
        }
    
        // 处理回调
        handle(handl) {
            if (this.state === 'PENDING') {
                console.log('handl, PENDING ' + this.name)
                this.handlers.push(handl)
            } else {
                if (this.state === 'FULFILLED') {
                    console.log(' FULFILLED  ' + this.name)
                    handl.onFulfilled(this.value)
                } else { // REJECT
                    console.log('reject')
                    handl.onReject(this.value)
                }
            }
        }
        // onResolve 1.改变状态, 2.并设置数据, 3.调用handlers里的回调
        onResolve(result) {
            console.log('in on resolve')
            // 改变状态
            this.state = 'FULFILLED'
            // 存入数据
            this.value = result
            this.handlers.forEach(handle => handle.onFulfilled(result));
            this.handlers = null;
        }
        // onReject 1.改变状态, 2.并设置数据, 3.调用handlers里的回调
        onReject(error) {
            this.state = 'REJECT'
            this.value = error
            this.handlers.forEach(handle => handle.onFulfilled(result));
            this.handlers = null;
        }
    
        // 要实现用.then链式调用,then返回了新的promise
        then(onFulfilled, onReject) {
            return new DiPromise((resolve, reject) => {
                this.done(() => {
                    try {
                        return resolve(onFulfilled(this.value))
                    } catch (err) {
                        return reject(err)
                    }
                }, () => {
                    try {
                        return resolve(onReject(this.value))
                    } catch (err) {
                        return reject(err)
                    }
                })
            })
        }
        
        // 做的事情:只是将回调传给this.handle
        done(onFulfilled, onReject) {
            setTimeout(() => {
                this.handle({
                    onFulfilled: onFulfilled,
                    onReject: onReject
                })
            }, 0)
        }
    }
    const p = new DiPromise(function excutor(resolve, reject) {
        setTimeout(() => {
            console.log('setTimeout')
            resolve('setTimeout')
        }, 1000)
    })
    p.then((res) => {
        console.log('then: ', res, this.name)
        return 'ffffffff'
    }).then((res) => {
        console.log('then: ', res, this.name)
        return '00000000$$$$$$'
    })

    控制台执行结果:

  • 相关阅读:
    阿里云容器服务多项重磅发布:高效智能、安全无界的新一代平台
    400倍加速, PolarDB HTAP实时数据分析技术解密
    先行一步,7大技术创新和突破,阿里云把 Serverless 领域的这些难题都给解了
    一图看懂云栖大会「云原生」重磅发布
    云栖发布|企业级互联网架构全新升级 ,助力数字创新
    3000份限量款云小宝手办全网首发,等你带回家!
    基于Delta lake、Hudi格式的湖仓一体方案
    git的clone和github的fork
    对vue的solt的理解
    对云信SDK的研究1
  • 原文地址:https://www.cnblogs.com/yadiblogs/p/10824983.html
Copyright © 2011-2022 走看看