学习promise过程

const l = console.log
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
// Object.defineProperty(x, then, {
//  get() {
//      return new Error()
//  }
// })
const resolvePromise = (promise2, x, resolve, reject) => {
    if (promise2 === x) {
        return reject(new TypeError('Chaining cycle detected for promise'))
    }
    if (typeof x === 'object' && x !== null || typeof x === 'function') {
        try {
            let then = x.then // Object.defineProperty
            if (typeof then === 'function') {
                // x.then 万一在报错
                then.call(x, (y) => {
                    resolve(y)
                }, (r) => {
                    reject(r)
                })
            } else {
                resolve(x)
            }
        } catch(e) {
            reject(e)
        }
    } else {
        resolve(x)
    }
}
class PPromise {
    constructor(executor) {
        this.status = PENDING
        this.value = void(0)
        this.reason = void(0)
        this.onResolvedCallbacks = []
        this.onRejectedCallbacks = []
        let resolve = (val) => {
            if (this.status === PENDING) {
                this.status = FULFILLED
                this.value = val
                this.onResolvedCallbacks.forEach(fn => fn())
            }
        }
        let reject = (reason) => {
            if (this.status === PENDING) {
                this.status = REJECTED
                this.reason = reason
                this.onRejectedCallbacks.forEach(fn => fn())
            }
        }
        try {
            executor(resolve, reject)
        } catch(error) {
            l(error)
            reject(error)
        }
    }
    then(onFulfilled, onRejected) {
        let promise2 = new PPromise((resolve, reject) => {
            // 如何将1000传到这
            if (this.status === FULFILLED) {
                setTimeout(() => { // 宏任务
                    try {
                        // 如何拿到return 1000的1000,然后传给下个then
                        let x = onFulfilled(this.value) // 1000
                        // then时返回promise才能接着.then
                        // resolve(x)
                        // 如果是promise就需要看x的value和status
                        // l('promise2???????', promise2)
                        resolvePromise(promise2, x, resolve, reject)
                    } catch(e) {
                        // 捕获 onFulfilled的异常,下个then接收
                        reject(e)
                    }
                })
            }
            if (this.status === REJECTED) {
                setTimeout(() => { // 宏任务
                    try {
                        // 如何拿到return 1000的1000,然后传给下个then
                        let x = onRejected(this.reason) // 1000
                        // then时返回promise才能接着.then
                        // resolve(x)
                        // 如果是promise就需要看x的value和status
                        // l('promise2???????', promise2)
                        resolvePromise(promise2, x, resolve, reject)
                    } catch(e) {
                        // 捕获 onFulfilled的异常,下个then接收
                        console.log('e:', e)
                        reject(e)
                    }
                })
            }
            if (this.status === PENDING) {
                this.onResolvedCallbacks.push(() => {
                    setTimeout(() => { // 宏任务 自己没法模拟微任务
                        try {
                            // 如何拿到return 1000的1000,然后传给下个then
                            let x = onFulfilled(this.value) // 1000
                            // then时返回promise才能接着.then
                            // resolve(x)
                            // 如果是promise就需要看x的value和status
                            // l('promise2???????', promise2)
                            resolvePromise(promise2, x, resolve, reject)
                        } catch(e) {
                            // 捕获 onFulfilled的异常,下个then接收
                            reject(e)
                        }
                    })
                })
                this.onRejectedCallbacks.push(() => {
                    setTimeout(() => { // 宏任务
                        try {
                            // 如何拿到return 1000的1000,然后传给下个then
                            let x = onRejected(this.reason) // 1000
                            // then时返回promise才能接着.then
                            // resolve(x)
                            // 如果是promise就需要看x的value和status
                            // l('promise2???????', promise2)
                            resolvePromise(promise2, x, resolve, reject)
                        } catch(e) {
                            // 捕获 onFulfilled的异常,下个then接收
                            reject(e)
                        }
                    })
                })
            }
        })
        return promise2
    }
}




const p = new PPromise((resolve, reject) => {
    resolve('haha')
})
const p2 = p.then((val) => {
    // throw new Error('then onFulfilled执行时如何catch,构造函数中的只能捕获同步异常') // 跟resolvePromise无关
    // return 1000
    return new Promise((resolve, reject) => {
        reject(100)
    })
})
p2.then(val => {
    l(val)
}, error => {
    l('hahah error')
    return 100
}).then(data => {
    l(data)
})
























你可能感兴趣的:(学习promise过程)