function Promise (executor){
// 2.1 Promise 的状态
// Promise 只有三种状态 1.pending 2.fulfilled 3.reject
this.state = 'pending'
const self = this
function resolve(value){
// 2.1.1 当promise 的状态为pending 状态
// 2.1.2 promise 可以转换状态
// 2.2.1 但是当promise 的状态已经变更 就不可改变
setTimeout(()=>{
if(self.state === 'pending'){
self.state = 'fulfilled'
self.data = value
}
})
}
function reject(reason){
// 2.1.1 当promise 的状态为pending 状态
// 2.1.2 promise 可以转换状态
// 2.2.1 但是当promise 的状态已经变更 就不可改变
setTimeout(()=>{
if(self.state === 'pending'){
self.state = 'rejected'
self.data = reason
}
})
}
}
// 2.2. then 方法
// promise 必须有.then 访问其当前值或最终值或 rejected 的原因
// 一个promise接收两个参数
Promise.prototype.then = function(onFulfilded,onRejected){
const self = this
if(self.state ==='fulfilded'){
}
}