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)
    }
}

猜你喜欢

转载自www.cnblogs.com/AFu-1993/p/12758648.html