认真看完这篇文章, 您能够本身封装一个简易但功能相对齐全的Promise, 还能够加深对Promise的理解es6
建议 : 看这篇文章以前但愿您数组
文章较长, 代码连贯性较强, 从简单开始入手, 读者能够按需选读promise
class Promise {
constructor (executor) {
if (typeof executor !== 'function')
throw new TypeError(`Promise resolver ${executor} is not a function`)
/* 默认状态 */
this.state = 'pending'
this.value = undefined
this.reason = undefined
/*
状态函数 resolve, reject
1.pending -> fulfilled, pending -> rejected
2.把数据储存到Promise实例上 this.value = value, this.reason = reason
*/
const resolve = value => {
if (this.state === 'pending') {
this.state = 'fulfilled'
this.value = value
}
}
const reject = reason => {
if (this.state === 'pending') {
this.state = 'rejected'
this.reason = reason
}
}
executor(resolve, reject)
}
then (onFulfilled, onRejected) {
if (this.state === 'fulfilled') {
onFulfilled(this.value)
}
if (this.state === 'rejected') {
onRejected(this.reason)
}
}
}
复制代码
ps : 测试工具为vsCode的Quokka插件bash
根据Promise
的状态函数res
和rej
,对应执行then
中的处理函数onFulfilled
和onRejected
异步
咱们都知道,Promise中的then函数的代码是异步执行的,而咱们写的这个并非,能够验证一下函数
显然这段代码是同步执行的,而咱们想要的输出顺序是 0 2 1
,因此咱们可使用setTimeout模拟这个异步工具
class Promise {
constructor (executor) { ... }
then (onFulfilled, onRejected) {
if (this.state === 'fulfilled') {
/* 使用setTimeout模拟异步 */
setTimeout(() => {
onFulfilled(this.value)
}, 0);
}
if (this.state === 'rejected') {
setTimeout(() => {
onRejected(this.reason)
}, 0);
}
}
}
复制代码
res/rej
为异步执行时, 咱们能够看到
then
是没有反应的
左边灰色小方块代表这行代码没有执行
为何呢? 那是由于当执行到then
函数的时候,res
为异步执行,因此状态仍是pending
,而咱们的then
函数里面尚未对状态为pending
的处理, 修改一下代码测试
class Promise {
constructor (executor) {
...
/* 状态函数异步执行时, 处理函数的存储列表 */
this.resolveCallBackList = []
this.rejectCallBackList = []
const resolve = value => {
if (this.state === 'pending') {
...
/* 若是有, 则执行处理函数列表里的函数 */
this.resolveCallBackList.length > 0
&& this.resolveCallBackList.forEach(e => e())
}
}
const reject = reason => {
if (this.state === 'pending') {
...
this.rejectCallBackList.length > 0
&& this.rejectCallBackList.forEach(e => e())
}
}
...
}
then (onFulfilled, onRejected) {
...
/* 状态为pending时, 把处理函数存储对相应的列表 */
if (this.state === 'pending') {
onFulfilled && this.resolveCallBackList.push( () => {
onFulfilled(this.value)
})
onRejected && this.rejectCallBackList.push( () => {
onRejected(this.reason)
})
}
}
}
复制代码
这样, 状态函数异步执行的时候也能够处理了, 能够简单理解为, 当状态为pending
时, 把处理函数onFulfilled/onRejected
存起来, 等状态函数res/rej
执行时, 自动执行对应的处理函数ui
当发生错误时, Promise
不会报错, 而是由失败的处理函数then函数的第二个函数
捕捉错误并处理, 若是咱们本身写的Promise
发生错误的话, 毫无心外是直接报错的, 就像这样this
既然执行时发生错误, 那么咱们就可使用try/catch
去捕获错误
class Promise {
constructor (executor) {
...
/* 使用try/catch捕获错误, 并执行reject, 改变状态为rejected */
try {
executor(resolve, reject)
} catch (error) {
this.state === 'pending' && reject(error)
}
}
then (onFulfilled, onRejected) { ... }
}
复制代码
then函数有两个特性
new Promise(res => res(0))
.then(value => {
console.log(value) // 0
return `1 fulfilled`
})
.then(value => {
console.log(value) // 1 fulfilled
})
复制代码
then
函数执行后返回一个Promise
实例, 该Promise
实例的状态由then
决定, 下一个then
函数根据返回的这个Promise
实例执行相应的处理函数, 画个图
then
的执行依赖于上一个
then
执行返回的
Promise
实例, 而这个
Promise
实例的数据由上一个
then
的处理函数
onFulfilled/onRejected
的
执行和其返回值
决定
若是按照字面意思去写代码
class Promise {
constructor (executor) { ... }
then (onFulfilled, onRejected) {
/* 一个新的Promise实例 */
const newPromise = new Promise ( (res, rej) => {})
...
return newPromise
}
}
复制代码
若是这样写, 是没意义的, 返回的Promise
实例的状态永远为pending
, 由于没有执行状态函数res/rej
, 所以也没法进行then
函数的链式调用
由于new Promise(executor)
的executor
函数是同步执行的, 因此咱们能够这样写
class Promise {
constructor (executor) { ... }
then (onFulfilled, onRejected) {
const newPromise = new Promise ( (res, rej) => {
/*
这部分的处理函数是同步执行的, 所以能够放在里面执行
同时还能经过res/rej改变返回的Promise实例的状态
*/
if (this.state === 'fulfilled') {
setTimeout(() => {
/* 拿处处理函数执行后的返回值 */
const value = onFulfilled(this.value)
/* 改变返回的Promise实例的状态并把数据传过去 */
res(value)
}, 0);
}
if (this.state === 'rejected') {
setTimeout(() => {
const reason = onRejected(this.reason)
res(reason)
}, 0);
}
if (this.state === 'pending') {
onFulfilled && this.resolveCallBackList.push( () => {
const value = onFulfilled(this.value)
res(value)
})
onRejected && this.rejectCallBackList.push( () => {
const reason = onRejected(this.reason)
res(reason)
})
}
})
return newPromise
}
}
复制代码
then
的链式调用完成了
ps : then
的处理函数返回值不是一个Promise
实例时, 不管fullfilled
仍是rejected
, 都是执行下一个then
函数的onFulfilled
当then
的处理函数返回值是一个Promise
实例时, 则下一个then
函数的执行, 所有由这个Promise
实例决定, 因此咱们须要使用checkReturnValueIfPromise
函数去判断一下返回值的类型并处理对应的状况
class Promise {
constructor (executor) { ... }
/*
promise -> Promise对象
target -> then的处理函数的返回值
res/rej -> 要返回的Promise实例的状态函数
*/
checkReturnValueIfPromise (promise, target, res, rej) {
if (target instanceof promise) {
/*
若是是Promise实例
则调用then函数,根据Promise实例的状态执行对应的处理函数
从而改变要返回的Promise实例的状态
若是下面的代码不能理解, 也能够写成这样
target.then( value => {
res(value)
}, reason => {
rej(reason)
} )
*/
target.then(res, rej)
} else {
res(target)
}
}
then (onFulfilled, onRejected) {
const newPromise = new Promise ( (res, rej) => {
if (this.state === 'fulfilled') {
setTimeout(() => {
const value = onFulfilled(this.value)
/* 调用检测函数并作相关处理 */
this.checkReturnValueIfPromise(Promise, value, res, rej)
}, 0);
}
if (this.state === 'rejected') {
setTimeout(() => {
const reason = onRejected(this.reason)
this.checkReturnValueIfPromise(Promise, reason, res, rej)
}, 0);
}
if (this.state === 'pending') {
onFulfilled && this.resolveCallBackList.push( () => {
const value = onFulfilled(this.value)
this.checkReturnValueIfPromise(Promise, value, res, rej)
})
onRejected && this.rejectCallBackList.push( () => {
const reason = onRejected(this.reason)
this.checkReturnValueIfPromise(Promise, reason, res, rej)
})
}
})
return newPromise
}
}
复制代码
对了, 还有一个与then
相似的方法catch
, 这个方法是专门处理rejected
状态的, 代码也就只有一句话
class Promise {
constructor () { ... }
then () { ... }
catch (onRejected) {
this.then(undefined, onRejected)
}
}
复制代码
返回一个fulfilled
状态的Promise
实例
class Promise {
constructor () { ... }
then () { ... }
catch () { ... }
static resolve (value) {
return new Promise( res => res(value))
}
}
复制代码
返回一个rejected
状态的Promise
实例
class Promise {
constructor () { ... }
then () { ... }
catch () { ... }
static resolve () { ... }
static reject (reason) {
return new Promise( (undefined, rej) => rej(reason))
}
}
复制代码
接收一个Promise
实例的数组promiseArray
, 返回一个Promise
实例, 返回的Promise
实例由promiseArray
中执行最快的Promise
实例决定
class Promise {
constructor () { ... }
then () { ... }
catch () { ... }
static resolve () { ... }
static reject () { ... }
static race (promiseArray) {
return new Promise ( (res, rej) => {
promiseArray.forEach( promise => {
promise.then(res, rej)
})
})
}
}
复制代码
功能描述太长了, 不懂的能够去看 阮一峰老师对于Promise.all的介绍
class Promise {
constructor () { ... }
then () { ... }
catch () { ... }
static resolve () { ... }
static reject () { ... }
static race () { ... }
static all (promiseArray) {
let count = 0,
resultArray = []
return new Promise( (res, rej) => {
promiseArray.forEach( promise => {
promise.then( value => {
count++
resultArray.push(value)
if (count === promiseArray.length) {
res(resultArray)
}
}, reason => {
rej(reason)
})
})
})
}
}
复制代码
谢谢浏览个人文章, 但愿你能学到东西