1.Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去 执行器接收参数会立即执行
2.Promise 中有三种状态分别为成功fulfilled 失败rejected 等待 pending
pending -> fulfilled pending -> rejected一旦状态确定就不可更改
3.resolve和reject函数是用来更改状态的
resolve: fulfilled
reject: rejected
4.then方法内部做的事情就判断状态 如果状态是成功 调用成功的回调函数 如果状态是失败调用失败回调函数 then方法是被定义在原型对象上的
5.then成功回调有一个参数 表示成功之后的值 then失败回调有一个参数 表示失败后的原因
myPromise.js
const PENDING = 'pending' // 等待
const FULFILLED = 'fulfilled' // 成功
const REJECTED = 'rejected'// 失败
class MyPromise {
constructor(executor) {
executor(this.resolve, this.reject)
}
status = PENDING
value = undefined
reason = undefined
resolve = value => {
if (this.status !== PENDING) return
// 将状态修改为成功
this.status = FULFILLED
// 保存成功之后的值
this.value = value
}
reject = reason => {
if (this.status !== PENDING) return
this.status = REJECTED
this.reason = reason
}
then(successCallback, failCallback) {
if (this.status === FULFILLED) {
successCallback(this.value)
} else if (this.status === REJECTED) {
failCallback(this.reason)
}
}
}
module.exports = MyPromise
index.js
const MyPromise = require('./myPromise')
let promise = new MyPromise((resolve, reject) => {
// resolve('成功')
reject('失败')
})
promise.then(value => {
console.log(value)
}, reason => {
console.log(reason)
})
网友评论