实现一个基本的Promise

// 定义Promise的三种状态常量
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

function MyPromise(executor) {
    this.status = PENDING   // 默认的时候的PENDING
    this.value = null    // 成功时候的值
    this.reason = null    // 失败时候的值
    this.onResolvedCallbacks = []   // 成功时候的回调
    this.onRejectedCallbacks = []   // 失败时候的回调

    let _this = this;
    // 内部的resolve方法
    function resolve(value) {
        if (_this.status === PENDING) {
            // 改变Promise状态,设置值
            _this.value = value
            _this.status = FULFILLED
            // 若是异步的情况下,需要调用它原来then中传入的函数
            _this.onResolvedCallbacks.forEach(fn => fn())
        }
    }

    // 内部的reject方法
    function reject(reason) {
        // 改变Promise状态,设置值
        if (_this.status === PENDING) {
            _this.reason = reason
            _this.status = REJECTED
            // 若是异步的情况下,需要调用它原来then中传入的函数
            _this.onRejectedCallbacks.forEach(fn => fn())
        }
    }

    try {
        // 执行Promise内的代码
        executor(resolve, reject)
    } catch (error) {
        // 执行中有错误的话捕捉
        reject(error)
    }
}

function resolvePromise(promise2, x, resolve, reject) {
    // 如果promise2, x和相等,那么就循环引用了,表示两个永远等不到结果(resolve/reject)
    if (promise2 === x) {
        reject(new Error('循环引用'))
        return;
    }
    let called = false; // 避免多次调用
    if (typeof x != null && (typeof x === 'function' || typeof x === 'object')) {
        try {
            let then = x.then;
            if (typeof then === 'function') {   // 如果x.then是一个函数(有可能包含在对象属性中不是函数),调用该then方法,
                then.call(x, y => {
                    // y有可能也是Promise,所以还需要继续resolvePromise
                    if(called) return;
                    called = true;
                    resolvePromise(promise2, y, resolve, reject);
                }, reason => {
                    reject(reason);
                })
            } else {
                //x是一个对象,也可以直接resolve
                if(called) return;
                called = true;
                resolve(x)
            }
        } catch (error) {
            if(called) return;
            called = true;
            reject(error)
        }
    } else {
        // 如果x是普通值的情况可以直接resolve
        resolve(x)
    }
}

MyPromise.prototype.then = function (onFulfilled, onRejected) {
    // 如果传入的onFulfilled, onRejected不是一个函数的话,那么要转换成函数,用于在链式调用时能获得上一个相同的值
    onFulfilled = onFulfilled === 'function' ? onFulfilled : value => value
    onRejected = onRejected === 'function' ? onRejected : reason => reason
    let _this = this;
    // PromiseA+规范中写明: then方法必须返回一个新的Promise
    let promise2 = new MyPromise(function (resolve, reject) {
        if (_this.status === PENDING) {
            // 如果调用then方法的时候,此时Promise的状态还是PENDING的话,那么把then中的两个回调函数传到内部的数组中存放
            // ,等到调用resolve/reject方法的时候再执行里面的代码
            _this.onResolvedCallbacks.push(function () {
                let x = onFulfilled(_this.value)
                resolvePromise(promise2, x, resolve, reject)
            })
            _this.onRejectedCallbacks.push(function () {
                let x = onRejected(_this.reason)
                resolvePromise(promise2, x, resolve, reject)
            })
        } else if (_this.status === FULFILLED) {
            // 此时Promise的状态是FULFILLED的话,那么直接调用传入的onFulfilled方法就行
            let x = onFulfilled(_this.value)
            // 1. 由于then中需要返回一个Promise,那么这个Promise要怎样才能被resolve或reject呢?
            // 2. 如果调用onFulfilled的返回值x也是一个Promise怎么办
            // 通过实现一个resolvePromise的方法来解决他们之间的resolve或者reject的问题。
            resolvePromise(promise2, x, resolve, reject)
        } else if (_this.status === REJECTED) {
            // 此时Promise的状态是REJECTED的话,那么直接调用传入的onRejected方法就行
            let x = onRejected(_this.reason)
            // 1. 由于then中需要返回一个Promise,那么这个Promise要怎样才能被resolve或reject呢?
            // 2. 如果调用onRejected的返回值x也是一个Promise怎么办
            // 通过实现一个resolvePromise的方法来解决他们之间的resolve或者reject的问题。
            resolvePromise(promise2, x, resolve, reject)
        }
    })
    return promise2;
}

module.exports = MyPromise

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容