Promise的实现

const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

function Promise(executor){
    let self = this
    self.state = PENDING
    self.value = null
    self.reason = null
    self.onFulfilledCallbacks = []
    self.onRejectedCallbacks = []

    function resolve(value) {
        if(self.state === PENDING) {
            self.state = FULFILLED
            self.value = value

            self.onFulfilledCallbacks.forEach(fulfilledCallback => fulfilledCallback())
        }
    }

    function reject(reason) {
        if(self.state === PENDING) {
            self.state = REJECTED
            self.reason = reason

            self.onRejectedCallbacks.forEach(rejectedCallback => rejectedCallback())
        }
    }

    try {
        executor(resolve, reject)
    } catch (reason) {
        reject(reason)
    }

}

Promise.prototype.then = function(onFulfilled, onRejected) {    
    let self = this
    let promise2 = null

    promise2 = new Promise((resolve, reject)=>{
        if(self.state === PENDING) {
            self.onFulfilledCallbacks.push(()=>{
                setTimeout(function(){
                    try {
                        let x = onFulfilled(self.value)
                        self.resolvePromise(promise2, x, resolve, reject)
                    } catch (reason) {
                        reject(reason)
                    }
                }, 0)
            })
            self.onRejectedCallbacks.push(()=>{
                setTimeout(function(){
                    try {
                        let x = onRejected(self.reason)
                        self.resolvePromise(promise2, x, resolve, reject)
                    } catch (reason) {
                        reject(reason)
                    }
                }, 0)
            })
        }
        if(self.state === FULFILLED) {
            setTimeout(function(){
                try {
                    let x = onFulfilled(self.value)
                    self.resolvePromise(promise2, x, resolve, reject)
                } catch (reason) {
                    reject(reason)
                }
            }, 0)
        }
        if(self.state === REJECTED) {
            setTimeout(function(){
                try {
                    let x = onRejected(self.reason)
                    self.resolvePromise(promise2, x, resolve, reject)
                } catch (reason) {
                    reject(reason)
                }
            }, 0)
        }
    })
    return promise2
}

Promise.prototype.resolvePromise = function(promise2, x, resolve, reject) {
    let self = this
    let called= false
    
    if(promise2 === x) {
        return reject(new TypeError('循环引用'))
    }

    if(x !== null && (Object.prototype.toString.call(x)==='[object Object]'||Object.prototype.toString.call(x)==='[object Function]')){
        try {
            let then = x.then

            if(typeof then === 'function') {
                then.call(x, y=>{
                    if(called) return
                    called = true
                    self.resolvePromise(promise2, y, resolve, reject)
                }, reason=>{
                    if(called) return
                    called = true
                    reject(reason)
                })
            } else {
                resolve(x)
            }
        } catch (reason) {
            if(called) return
            called = true
            reject(reason)
        }
    } else {
        resolve(x)
    }
}

module.exports = Promise

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值