原生JS实现Promise

为了能完成一周至少一更的计划,还没有写讲解,先放上源码。为了便于理解,我把代码调整成相对精炼。最近忙着学习算法和Python,我会在空暇时间抓紧写讲解。希望我能够帮到你,感谢。

源码

function MyPromise(executor) {
    var self = this
    self.status = 'pending'
    self.onResolvedCallback = []
    self.onRejectedCallback = []

    function resolve(value) {
        setTimeout(function () { // 异步执行所有的回调函数
            if (self.status === 'pending') {
                self.status = 'resolved'
                self.data = value
                for (var i = 0; i < self.onResolvedCallback.length; i++) {
                    self.onResolvedCallback[i](value)
                }
            }
        })
    }

    function reject(reason) {
        setTimeout(function () { // 异步执行所有的回调函数
            if (self.status === 'pending') {
                self.status = 'rejected'
                self.data = reason
                for (var i = 0; i < self.onRejectedCallback.length; i++) {
                    self.onRejectedCallback[i](reason)
                }
            }
        })
    }

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

MyPromise.prototype.then = function (onResolved, onRejected) {
    var self = this
    var promise2
    onResolved = typeof onResolved === 'function' ? onResolved : function (v) {
        return v
    }
    onRejected = typeof onRejected === 'function' ? onRejected : function (r) {
        throw r
    }

    if (self.status === 'resolved') {
        return promise2 = new MyPromise(function (resolve, reject) {
            setTimeout(function () { // 异步执行onResolved
                try {
                    var x = onResolved(self.data)
                    resolve(x)
                } catch (reason) {
                    reject(reason)
                }
            })
        })
    }

    if (self.status === 'rejected') {
        return promise2 = new MyPromise(function (resolve, reject) {
            setTimeout(function () { // 异步执行onRejected
                try {
                    var x = onRejected(self.data)
                    resolve(x)
                } catch (reason) {
                    reject(reason)
                }
            })
        })
    }

    if (self.status === 'pending') {
        // 这里之所以没有异步执行,是因为这些函数必然会被resolve或reject调用,而resolve或reject函数里的内容已是异步执行,构造函数里的定义
        return promise2 = new MyPromise(function (resolve, reject) {
            self.onResolvedCallback.push(function (value) {
                try {
                    var x = onResolved(value)
                    resolve(x)
                } catch (r) {
                    reject(r)
                }
            })

            self.onRejectedCallback.push(function (reason) {
                try {
                    var x = onRejected(reason)
                    resolve(x)
                } catch (r) {
                    reject(r)
                }
            })
        })
    }
}

MyPromise.prototype.catch = function (onRejected) {
    return this.then(null, onRejected)
}

测试

let myPromise1 = new MyPromise((resolve,reject)=>{
 setTimeout(()=>{ resolve('==resolve==')},100)}).then((v)=>{
    console.log('成功');
    console.log(v);},(r)=>{
    console.log('失败');
    console.log(r);})
let myPromise2 = new MyPromise((resolve,reject)=>{
 setTimeout(()=>{ reject('==resolve==')},100)}).catch((r)=>{console.log(r)})

你可能感兴趣的:(JavaScript)