手写简易版的Promise

/*
首先我们创建了三个常量用于表示状态,对于经常使用的一些值都应该通过常量来管理,便于开发及后期维护
*/
const PENDING = 'pending'
const RESOLVED = 'resolved'
const REJECTED = 'rejected'
function MyPromise(fn) {
//创建了常量 that ,因为代码可能会异步执行,用于获取正确的this 对象
const that = this
//一开始的状态是pending
that.state = PENDING
//value 变量用于保存 resolve 或者 reject 中传入的值
that.value = null
//resolvedCallbacks 和 rejectedCallbacks 用于保存 then 中的回调,因为当执行完 Promise 时状态可能还是等待中,这时候应该把 then 中的回调保存起来用于状态改变时使用
that.resolvedCallbacks = []
that.rejectedCallbacks = []
// 待完善 resolve 和 reject 函数
function resolve(value) {
//判断当前状态是否为等待中,因为规范规定只有等待态才可以改变状态
if (that.state === PENDING) {
//将当前状态更改为对应状态,并且将传入的值赋值给 value
that.state = RESOLVED
that.value = value
//遍历回调数组并执行
that.resolvedCallbacks.map(cb => cb(that.value))
}
}
function reject(value) {
//判断当前状态是否为等待中,因为规范规定只有等待态才可以改变状态
if (that.state === PENDING) {
//将当前状态更改为对应状态,并且将传入的值赋值给 value
that.state = REJECTED
that.value = value
//遍历回调数组并执行
that.rejectedCallbacks.map(cb => cb(that.value))
}
}
// 待完善执行 fn 函数
try {
//执行传入的参数并且将之前两个函数当做参数传进去
fn(resolve, reject)
} catch (e) {
//可能执行函数过程中会遇到错误,需要捕获错误并且执行 reject 函数
reject(e)
}
}
//then函数的实现
MyPromise.prototype.then = function(onFulfilled,onRejected) {
const that = this
//判断两个参数是否为函数类型
//当参数不是函数类型时,需要创建一个函数赋值给对应的参数,同时也实现了透传
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v
onRejected = typeof onRejected === 'function' ? onRejected : r => {
throw r
}
//当状态不是等待态时,就去执行相对应的函数。如果状态是等待态的话,就往回调函数中 push 函数
if (that.state === PENDING) {
that.resolvedCallbacks.push(onFulfilled)
that.rejectedCallbacks.push(onRejected)
}
if (that.state === RESOLVED) {
onFulfilled(that.value)
}
if (that.state === REJECTED) {
onRejected(that.value)
}
}