首页 > 其他分享 >随便写一个promise

随便写一个promise

时间:2022-10-22 20:15:45浏览次数:66  
标签:function callback2 resolve 一个 value 随便 callback state promise

function myPromise(fn){
    this.state = 'Pending'
    this.value
    this.resolve = function(){
        if(this.state != 'Pending'){
            return
        }
        this.state = 'fulfilled'
        this.value = arguments
    }
    this.reject = function(){
        this.state = 'rejected'
        this.value = arguments
    }
    this.then = this.catch = function(callback1, callback2){
        let callback = (this.state == 'fulfilled' ? callback1 : callback2)
        let result = callback(...this.value)
        return new myPromise((resolve)=>{ resolve(result) })
    }  
    this.finally = function(callback) {
        callback()
    }
    fn(this.resolve.bind(this), this.reject.bind(this))
}

 

标签:function,callback2,resolve,一个,value,随便,callback,state,promise
From: https://www.cnblogs.com/jiangxiaoxi/p/16817173.html

相关文章