从零手撕Promise,掌握Promise的实现原理(12)之Promise的catch、resolve、reject方法
「这是我参与11月更文挑战的第14天,活动详情查看:2021最后一次更文挑战」。
传送门
从零手撕Promise,掌握Promise的实现原理(1)之promise基本结构的实现
从零手撕Promise,掌握Promise的实现原理(2)之基础版本的promise实现
从零手撕Promise,掌握Promise的实现原理(3)之回调地狱是什么
从零手撕Promise,掌握Promise的实现原理(4)之then方法链式调用的初步实现
从零手撕Promise,掌握Promise的实现原理(5)之then方法链式调用的进阶实现
从零手撕Promise,掌握Promise的实现原理(6)之then方法的回调为什么是异步微任务
从零手撕Promise,掌握Promise的实现原理(7)then方法链式调用之核心方法resolvePromise
从零手撕Promise,掌握Promise的实现原理(8)then方法链式调用之核心方法resolvePromise再探究
从零手撕Promise,掌握Promise的实现原理(9)then方法链式调用之核心方法resolvePromise完全体
从零手撕Promise,掌握Promise的实现原理(10)then方法完全体
从零手撕Promise,掌握Promise的实现原理(11)之测试完全体Promise是否符合PromiseA+
回顾
经过前几篇文章的介绍,我们的Promise已经经过了,Promise A+规范的测试,现在我们的Promise,长这样。
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
const resolvePromise = (promise, x, resolve, reject) => {
//*******************************************************判断x与promsie是否是同一个promise,防止进入死循环*********************//////*********
if(x === promise) throw new TypeError('Chaining cycle detected for promise #<Promise>')
// 1.首先判断`x`是基础类型数据,还是引用类型,基础类型的数据直接`resolve`,即可。
if(x !== null && /^(object|function)$/.test(typeof x)){
let then
// 2. 如果是引用类型的数据,尝试获取`x`上的`then`属性(`x.then`),如果在获取属性的时候报异常则`reject`
try{
then = x.then
}catch(e){
reject(e)
}
//3. 判断`then`是否是函数,如果是一个函数则我们认定它为`Promise`,如果不是则`resolve`
if(typeof then === 'function'){
let called = false //**************************************这里加了变量*************************//////
try{
then.call(x, (y) => {
if(called) return //**************************************这里加了变量*************************//////
called = true
resolvePromise(promise, y, resolve, reject)
},(r) => {
if(called) return//**************************************这里加了变量*************************//////
reject(r)
})
}catch(e){
if(called) return//**************************************这里加了变量*************************//////
reject(e)
}
}else{
resolve(x)
}
}else{
//基础类型数据直接resolve
resolve(x)
}
}
class Promise{
constructor(executor){
this.state = PENDING
this.value = undefined
this.reason = undefined
//存放onFulfilled
this.onResolvedCallbacks = []
//存放onRejected
this.onRejectedCallbacks = []
const resolve = (value) => {
if (this.state === PENDING) {
this.value = value
this.state = FULFILLED
//promise实例状态改变后调用暂存的onFulfilled
this.onResolvedCallbacks.forEach(fn => fn())
}
}
const reject = (reason) => {
if (this.state === PENDING) {
this.reason = reason
this.state = REJECTED
//promise实例状态改变后调用的onRejected
this.onRejectedCallbacks.forEach(fn => fn())
}
}
try {
//executor函数执行过程中出错,将会导致Promise失败
executor(resolve,reject)
} catch (error) {
reject(error)
}
}
then(onFulfilled, onRejected){
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v
onRejected = typeof onRejected === 'function' ? onRejected : err => { throw err }
let promise = new Promise((resolve, reject) => {
switch(this.state){
case FULFILLED:
setTimeout(() => {
try{
let x = onFulfilled(this.value)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
break
case REJECTED:
setTimeout(() => {
try{
let x = onRejected(this.reason)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
break
default:
this.onResolvedCallbacks.push(() => {
setTimeout(() => {
try{
let x = onFulfilled(this.value)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
})
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try{
let x = onRejected(this.reason)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
})
}
})
return promise
}
}
Promsie的其他方法
我们完成了,Promsie的主体后,还有一些其他的方法(这些方法并不属于Promsie A+中规定的),例如catch、reject、resolve等等,这章节主要介绍这三个方法。
catch方法
catch方法很简单,catch方法是原型上的方法,它的作用主要是捕获Promise失败的,其实内部实现就是调用then方法的传入第二个参数(第一个参数传null),具体实现如下:
catch(onRejected){
return this.then(null, onRejected)
}
resolve方法
resolve方法是静态方法,它的作用是返回一个成功的Promsie。
-
注意 但是并不是说
resolve方法返回的一定是成功,他也会返回失败的Promise,看下面这张图片。 假如说给resolve传递了一个Promise,那resolve返回的Promise会由嵌套的这个Promise,去决定。
resolve实现如下,我们还需要改动我们Promsie的resolve方法。
const resolve = (value) => {
//如果发现value是一个Promise,我们需要调用value的then方法去递归解析
if(value instanceof Promise){
return value.then(resolve,reject); // 递归解析
}
if (this.state === PENDING) {
this.value = value
this.state = FULFILLED
//promise实例状态改变后调用暂存的onFulfilled
this.onResolvedCallbacks.forEach(fn => fn())
}
}
static rsolve(value){
return new Promise((resolve,reject)=>{
resolve(value);
})
}
reject方法
reject方法十分的简单,他也是静态方法,它的作用是返回一个失败的Promise,实现如下。
static reject(reason){
return new Promise((resolve,reject)=>{
reject(reason);
})
}
截至目前的Promise
现在我们的Promise长这样.
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
const resolvePromise = (promise, x, resolve, reject) => {
//*******************************************************判断x与promsie是否是同一个promise,防止进入死循环*********************//////*********
if(x === promise) throw new TypeError('Chaining cycle detected for promise #<Promise>')
// 1.首先判断`x`是基础类型数据,还是引用类型,基础类型的数据直接`resolve`,即可。
if(x !== null && /^(object|function)$/.test(typeof x)){
let then
// 2. 如果是引用类型的数据,尝试获取`x`上的`then`属性(`x.then`),如果在获取属性的时候报异常则`reject`
try{
then = x.then
}catch(e){
reject(e)
}
//3. 判断`then`是否是函数,如果是一个函数则我们认定它为`Promise`,如果不是则`resolve`
if(typeof then === 'function'){
let called = false //**************************************这里加了变量*************************//////
try{
then.call(x, (y) => {
if(called) return //**************************************这里加了变量*************************//////
called = true
resolvePromise(promise, y, resolve, reject)
},(r) => {
if(called) return//**************************************这里加了变量*************************//////
reject(r)
})
}catch(e){
if(called) return//**************************************这里加了变量*************************//////
reject(e)
}
}else{
resolve(x)
}
}else{
//基础类型数据直接resolve
resolve(x)
}
}
class Promise{
constructor(executor){
this.state = PENDING
this.value = undefined
this.reason = undefined
//存放onFulfilled
this.onResolvedCallbacks = []
//存放onRejected
this.onRejectedCallbacks = []
const resolve = (value) => {
//如果发现value是一个Promise,我们需要调用value的then方法去递归解析
if(value instanceof Promise){
return value.then(resolve,reject); // 递归解析
}
if (this.state === PENDING) {
this.value = value
this.state = FULFILLED
//promise实例状态改变后调用暂存的onFulfilled
this.onResolvedCallbacks.forEach(fn => fn())
}
}
const reject = (reason) => {
if (this.state === PENDING) {
this.reason = reason
this.state = REJECTED
//promise实例状态改变后调用的onRejected
this.onRejectedCallbacks.forEach(fn => fn())
}
}
try {
//executor函数执行过程中出错,将会导致Promise失败
executor(resolve,reject)
} catch (error) {
reject(error)
}
}
then(onFulfilled, onRejected){
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v
onRejected = typeof onRejected === 'function' ? onRejected : err => { throw err }
let promise = new Promise((resolve, reject) => {
switch(this.state){
case FULFILLED:
setTimeout(() => {
try{
let x = onFulfilled(this.value)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
break
case REJECTED:
setTimeout(() => {
try{
let x = onRejected(this.reason)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
break
default:
this.onResolvedCallbacks.push(() => {
setTimeout(() => {
try{
let x = onFulfilled(this.value)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
})
this.onRejectedCallbacks.push(() => {
setTimeout(() => {
try{
let x = onRejected(this.reason)
resolvePromise(promise, x, resolve, reject)
} catch(e){
reject(e)
}
})
})
}
})
return promise
}
catch(onRejected){
return this.then(null, onRejected)
}
static rsolve(value){
return new Promise((resolve,reject)=>{
resolve(value);
})
}
static reject(reason){
return new Promise((resolve,reject)=>{
reject(reason);
})
}
}
后续文章继续完成Promsie其他的方法
传送门
从零手撕Promise,掌握Promise的实现原理(1)之promise基本结构的实现
从零手撕Promise,掌握Promise的实现原理(2)之基础版本的promise实现
从零手撕Promise,掌握Promise的实现原理(3)之回调地狱是什么
从零手撕Promise,掌握Promise的实现原理(4)之then方法链式调用的初步实现
从零手撕Promise,掌握Promise的实现原理(5)之then方法链式调用的进阶实现
从零手撕Promise,掌握Promise的实现原理(6)之then方法的回调为什么是异步微任务
从零手撕Promise,掌握Promise的实现原理(7)then方法链式调用之核心方法resolvePromise
从零手撕Promise,掌握Promise的实现原理(8)then方法链式调用之核心方法resolvePromise再探究
从零手撕Promise,掌握Promise的实现原理(9)then方法链式调用之核心方法resolvePromise完全体