关于前端:从零实现一个-promise

41次阅读

共计 1834 个字符,预计需要花费 5 分钟才能阅读完成。

切入点从场景形容登程,即先定义好咱们要实现的性能

执行器函数

构造函数入参 executor 自执行函数。会在在 new 的时候同步执行,传入 resolve 和 reject 状态扭转函数。自执行函数外部依据异步工作执行后果(胜利或失败)调用状态扭转函数,把状态传递给后续的 then。

状态转化

promise 有三种状态,默认是 pending,胜利之后为 fulfilled,失败之后为 failed。且状态一旦产生扭转,之后不可再扭转也不可逆

then 办法

then 办法承受两个函数,示意胜利、失败状态下的回调函数。回调函数中 return 中值会当作参数传给后续的 then。以此实现链式调用。

判断一个变量是否为 promise 须要两个条件,首先要是对象,而后对象上有 then 办法,以此得出 then 执行的后果也是一个 promise

实现

class Promise {constructor(executor){
        this.status = 'pending',
        this.value = undefined;
        this.reason = undefined;
        this.onFulfilled =undefined;
        this.onRejected = undefined;
        this.onFulfilledList = [];
        this.onRejectedList = [];
        try {executor(this.resolve, this.reject);
        } catch(e) {this.reject(e)
        }
    }
    resolve(value){if(this.status == 'pending') {
            this.status = 'fullfilled';
            this.value = value;
            this.onFulfilledList.forEach(item => item())
        }
    }
    reject(reason){if(this.status == 'pending') {
            this.status = 'rejected';
            this.reason = reason;
            this.onRejectedList.forEach(item => item())
        }
    }
    then(onFulfilled, onRejected){
        // then 之后要返回一个新的 promise
        let result;
        if(this.status == 'fullfilled' && onFulfilled) {result = onFulfilled(this.value)
            return Promise.resolve(result);
        }

        if(this.status == 'rejected' && onRejected) {result = onRejected(this.reason);
            return Promise.resolve(result);
        }

        if(this.status == 'pending') {onFulfilled && this.onFulfilledList.push(()=>onFulfilled(this.value));
            onRejected && this.onRejectedList.push(() => onRejected(this.reason));
        }
    }
}

Promise.resolve = function(value) {if(typeof value == 'object' && value.then) {return value;} else {return new Promise((resolve, reject)=>{resolve(value)
        })
    }
}

Promise.all = function(list) {return new Promise((resolve,reject) => {let result = [];
        let count = 0;
        for(let i = 0; i < list.length; i++)  {if(typeof list[i] == 'object' && list[i].then) {Promise.resolve(list[i]).then(data =>{result[i] = data;
                    count++;
                },reject)
            }else {result[i] = list[i];
                count++;
            }
        }
        if(count == list.length) {resolve(result);
        } 
    })
   
}

// Promise.race  同理,只有有一个胜利,全副  resolve
// Promise.finally 不论成功失败,传递的回调函数都会执行,执行之后依然能够跟 then

(完)

正文完
 0