简版 promise

47 阅读1分钟
  1. 初始状态 pending
  2. 执行了 resolve Promise状态变成 fulfilled
  3. 执行了 reject Promise状态变成 rejected
  4. 状态不可逆
  5. Promise中有throw =》 rejected
  6. then接收两个回调 成功和失败
  7. fulfilled 执行成功的回调 rejected执行失败的回调
  8. 存在定时器需要定时器执行结束后才执行then
  9. then函数是异步执行的微任务
  10. then 支持链式调用,下次then接收上次then的返回值
  11. all实现
class myPromise{
    constructor(executor){
        this.initValue()
        this.initBind()
        try {
            executor(this.resolve,this.reject)
        } catch (error) {
            this.reject(error)
        }
    }
    initValue() {
        this.PromiseState = 'pending'
        this.PromiseResult = null
        this.onFulfilledCallback = []
        this.onRejectedCallback = []
    }
    initBind() {
        this.resolve = this.resolve.bind(this)
        this.reject = this.reject.bind(this)
    }
    resolve(result) {
        if (this.PromiseState !== 'pending') return
        this.PromiseState = 'fulfilled'
        this.PromiseResult = result
        while (this.onFulfilledCallback.length) {
            const { onFulfilled, resolve } = this.onFulfilledCallback.shift()
            const result = onFulfilled(this.PromiseResult)
            resolve(result)
        }
    }
    reject(reason){
        if (this.PromiseState !== 'pending') return
        this.PromiseState = 'rejected'
        this.PromiseResult = reason
        while (this.onRejectedCallback.length) {
            const { onRejected, reject } = this.onRejectedCallback.shift()
            const resultErr = onRejected(this.PromiseResult)
            reject(resultErr)
        }
    }
    then(onFulfilled,onRejected){
        onFulfilled = typeof onFulfilled === "function" ? onFulfilled : val => val
        onRejected = typeof onRejected === "function" ? onRejected : val => val
        var thenPromise = new myPromise((resolve, reject) => {
                queueMicrotask(() => {
                    if (this.PromiseState === 'fulfilled') {
                        let result = onFulfilled(this.PromiseResult)
                        resolve(result)
                    }else if(this.PromiseState === 'rejected'){
                        let err = onRejected(this.PromiseResult)
                        reject(err)
                    }else if(this.PromiseState === 'pending') {
                        this.onFulfilledCallback.push({ onFulfilled:onFulfilled.bind(this), resolve: resolve.bind(this) })
                        this.onRejectedCallback.push({ onRejected: onRejected.bind(this), reject: reject.bind(this) })
                    }
                })
            })
        return thenPromise
    }
    static all(promiseList) {
        const result = []
        let count = 0
        return new myPromise((resolve, reject) => {
            const addData = (index, value) => {
                result[index] = value
                count ++
                if (count === promiseList.length) {
                    resolve(result)
                }
            }
            for (const [index, promise] of promiseList.entries()) {
                if (promise instanceof myPromise) {
                    promise.then(
                        res => { addData(index, res)},
                        err => reject(err)
                    )
                }else{
                    addData(index, promise)
                }
            }
            // promiseList.forEach((promise, index) => {
            //     if (promise instanceof myPromise) {
            //         promise.then(
            //             res => { addData(index, res)},
            //             err => reject(err)
            //         )
            //     }else{
            //         addData(index, promise)
            //     }
            // });
        })
    }
}