1.Promise 的构成
Promise有三个状态Pending,Fulfilled,Rejected,表示响应中,成功和失败。
Promise实例有then(),resolve(),reject()这三个方法可以调用。
Promise有all(),race(),allSettled(),any()这四个静态方法可以使用。
- 在
new Promise(fn)的时候我们通常会传入一个函数,并且函数带有两个参数resolve与reject,这两个参数也是就是我们的实例方法。
2.基础形态
- 我们还需要给有初始化值的方法与初始化
initValue(),改变this指向的方法initBind(),下面我们使用Class来写出大概样子。
class Promise {
static PENDING = 'PENDING'
static SUCCESS = 'FULFILLED'
static FAIL = 'REJECTED'
static NULL = null
constructor(executor) {
this.initValue()
this.initBind()
try {
executor(this.resolve, this.reject)
} catch (e) {
reject this.reject(e)
}
}
initValue() {
this.PromiseState = Promise.PENDING
this.PromiseResult = Promise.NULL
this.onFulfillCallbacks = []
this.onRejectCallbacks = []
}
initBind() {
this.resolve = this.resolve.bind(this)
this.reject = this.reject.bind(this)
}
resolve(value) {
if (this.PromiseState !== Promise.PENDING) return
this.PromiseState = Promise.SUCCESS
this.PromiseResult = value
while (this.onFulfillCallbacks.length) {
this.onFulfillCallbacks.shift()(this.PromiseResult)
}
}
reject(reason) {
if (this.PromiseState !== Promise.PENDING) return
this.PromiseState = Promise.FAIL
this.PromiseResult = reason
while (this.onRejectCallbacks.length) {
this.onRejectCallbacks.shift()(this.PromiseResult)
}
}
}
then(onFulfilled, onRejected) {}
static all(promises) {}
static race(promises) {}
static allSettled(promises) {}
static any(promises) {}
3.then 方法
then 方法可以传入两个参数,第一个参数是状态为FULFILLED后的会执行回调函数,第二个参数是状态为REJECTED时会执行的回调函数。
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value
onRejected = typeof onRejected === 'function' ? onRejected : reason => throw reason
const thenPromise = new Promise((resolve, reject) => {
const resolvePromise = cb => {
queueMicrotask(() => {
try {
const x = cb(this.PromiseResult)
if (thenPromise === x) {
} else if (x instanceof Promise) {
x.then(resolve, reject)
} else {
resolve(x)
}
} catch (e) {
}
})
}
if (this.PromiseState === Promise.SUCCESS) {
resolvePromise(onFulfilled)
} else if (this.PromiseState === Promise.FAIL) {
resolvePromise(onRejected)
} else if (this.PromiseState === Promise.PENDING) {
this.onFulfillCallbacks.push(resolvePromise.bind(this, onFulfilled))
this.onRejectCallbacks.push(resolvePromise.bind(this, onRejected))
}
})
return thenPromise
4.all 方法
all方法有一个参数,是一个数组,数组里的元素都为promise,当所有的promise都为成功时才能返回一个数组,元素是所有成功的结果,只要有一个promise失败就会将这个失败的结果返回。
all(promises) {
const result = []
let count = 0
return new Promise((resolve, reject) => {
const addData = (index, value) => {
result[index] = value count++
if (count === promises.length) {
resolve(result)
}
}
promises.forEach((promise, index) => {
if (promise instanceof Promise) {
promise.then(res => {
addData(index, res)
}, err => {
reject(err)
})
} else {
addData(index, promise)
}
})
})
}
5.race 方法
- 参数与
all方法一样,传入多个promise组成的数组,返回的结果第一个有结果的promise,不管是成功还是失败。
race(promises) {
return new Promise((resolve, reject) => {
promises.forEach(promise => {
if (promise instanceof Promise) {
promise.then(res => {
resolve(res)
}, err => {
reject(err)
})
} else {
resolve(promise)
}
})
})
}
6.allSettled 方法
- 参数为多个
promise组成的数组,返回的所有的promise状态和结果,不管是成功还是失败。
allSettled(promises) {
return new Promise((resolve, reject) => {
const res = []
let count = 0
const addData = (status, value, i) => {
res[i] = { status, value }
count++
if (count === promises.length) {
resolve(res)
}
}
promises.forEach((promise, i) => {
if (promise instanceof Promise) {
promise.then(res => {
addData('fulfilled', res, i)
}, err => {
addData('rejected', err, i)
})
} else {
当数组元素部位 Promise 时,默认成功,结果为元素本身
addData('fulfilled', promise, i)
}
})
})
}
7.any 方法
- 参数为多个
promise组成的数组,只要有一个promise状态为成功,那么就算成功,返回第一个成功promise的结果,如果所有的promise都失败,那么就会报错。
any(promises) {
return new Promise((resolve, reject) => {
let count = 0
promises.forEach((promise) => {
promise.then(val => {
resolve(val)
}, err => {
count++
if (count === promises.length) {
reject(new AggregateError('All promises were rejected'))
}
})
})
})
}