• promise实现(未实现微任务)


    promise实现

    promsie接收一个函数作为参数
    有:

    function Promise(fn){
        fn()
    }
    

    这个函数fn接收两个参数用来控制Prmosise实例的状态,这两个参数在Promise内部传给fn
    有:
    有:

    function Promise(fn){
        const res = function(){}
        const rej = function(){}
        fn(res,rej)
    }
    

    res能改变Promise实例的状态所以,Promise有一个公有属性status,并且res、rej可以改变他们
    有:

    function Promise(fn){
        this.status = 'padding'
        const res = this.res.bind(this)
        const rej = this.rej.bind(this)
        fn(res,rej)
    }
    Promise.prototype.res = function(){
        this.status = 'res'
    }
    Promise.prototype.rej = function(){
        this.status = 'rej'
    }
    

    Promise有一个then方法,当status改变的时候会执行,否则会等待status改变。
    所以,其实then做了两件事情,检查status是否改变,执行回调。
    有:

    function Promise(fn){
        this.status = 'padding'
        const res = this.res.bind(this)
        const rej = this.rej.bind(this)
        fn(res,rej)
    }
    Promise.prototype.res = function(){
        this.status = 'res'
    }
    Promise.prototype.rej = function(){
        this.status = 'rej'
    }
    Promise.prototype.then = function(callback_res,callback_rej){
        if(this.status==='res'){
            callback_res()
        }
        if(this.status==='rej'){
            callback_rej()
        }
    }
    

    上面当status没有改变的时候,then的回调无法执行。所以在status改变后需要将then的回调执行,在status没有执行的时候可以先把它存在Promise实例对象中。
    有:

    function Promise(fn){
        this.status = 'padding'
        const res = this.res.bind(this)
        const rej = this.rej.bind(this)
        const res_fn = []/*  */
        const rej_fn = []/*  */
        fn(res,rej)
    }
    Promise.prototype.res = function(){
        this.status = 'res'
        this.res_fn.forEach(function(fn){ /*  */
            if(typeof fn === 'function'){
                fn()
            }
        })
    }
    Promise.prototype.rej = function(){
        this.status = 'rej'
        this.rej_fn.forEach(function(fn){ /*  */
            if(typeof fn === 'function'){
                fn()
            }
        })
    }
    Promise.prototype.then = function(callback_res,callback_rej){
        if(this.status==='res'){
            callback_res()
        }
        if(this.status==='rej'){
            callback_rej()
        }
        if(this.status==='padding'){/*  */
            this.res_fn.push(callback_res)
            this.rej_fn.push(callback_rej)
        }
    }
    

    res、rej分别接收一个参数,该参数将分别作为callback_res、callback_rej的参数。
    有:

    function Promise(fn){
        this.status = 'padding'
        const res = this.res.bind(this)
        const rej = this.rej.bind(this)
        const res_value
        const rej_value
        const res_fn = []
        const rej_fn = []
        fn(res,rej)
    }
    Promise.prototype.res = function(){
        this.status = 'res'
        this.res_fn.forEach(function(fn){ 
            if(typeof fn === 'function'){
                fn()
            }
        })
    }
    Promise.prototype.rej = function(){
        this.status = 'rej'
        this.rej_fn.forEach(function(fn){ 
            if(typeof fn === 'function'){
                fn()
            }
        })
    }
    Promise.prototype.then = function(callback_res,callback_rej){
        if(this.status==='res'){
            callback_res()
        }
        if(this.status==='rej'){
            callback_rej()
        }
        if(this.status==='padding'){
            this.res_fn.push(callback_res)
            this.rej_fn.push(callback_rej)
        }
    }
    
  • 相关阅读:
    sniffer 和 debug flow
    如何认识TOS----DSCP 对照表
    NAT alg 和 ASPF
    使用refind引导多系统
    Backdooring a OS VM
    exit(0)与exit(1),return三者区别(详解)
    Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)
    2017年中国大学生程序设计竞赛-中南地区赛暨第八届湘潭市大学生计算机程序设计大赛题解&源码(A.高斯消元,D,模拟,E,前缀和,F,LCS,H,Prim算法,I,胡搞,J,树状数组)
    高斯消元法(Gauss Elimination)【超详解&模板】
    2015 计蒜之道 初赛(4)爱奇艺的自制节目(枚举 贪心)
  • 原文地址:https://www.cnblogs.com/AFu-1993/p/12758648.html
Copyright © 2020-2023  润新知