简单版
手动实现Promise功能
class LikePromise {constructor(exec) {this._resolve = nullexec(this.resolve.bind(this), null)}resolve(value) {setTimeout(() => {this._resolve(value)}, 0)}then(resolveFunc) {this._resolve = resolveFunc}}var p1 = new LikePromise((resolve) => {resolve(10)})p1.then((data) => {console.log('--data--', data);})
综合版
class MockPromise {constructor(exec) {this.status = 'pending'this.value = nullthis.resolvedTasks = []this.rejectedTasks = []this._resolve = this._resolve.bind(this)this._reject = this._reject.bind(this)// catch err 到 then 中的 onRejected 的reasontry {exec(this._resolve, this._reject)} catch (error) {this._reject(error)}}_resolve(value) {if (this.status === MockPromise.PENDING) {this.status = MockPromise.FULFILLEDthis.value = valuethis.resolvedTasks.forEach((fn) => {fn(value)})}}_reject(reason) {if (this.status === MockPromise.PENDING) {this.status = MockPromise.REJECTEDthis.value = reasonthis.rejectedTasks.forEach((fn) => {fn(reason)})}}then(onFulfilled, onRejected) {if (typeof onFulfilled !== 'function') {onFulfilled = (value) => {return value}}if (typeof onRejected !== 'function') {onRejected = (resaon) => {throw resaon}}/*** 实现链式调用 return 一个promise* 判断 onFulfilled() 的返回值类型就好了*/return new MockPromise((resolve, reject) => {if (this.status === MockPromise.FULFILLED) {setTimeout(() => {try {const res = onFulfilled(this.value)if (res instanceof MockPromise) {res.then(value => {resolve(value)})} else {resolve(res)}} catch (error) {reject(error)}})}if (this.status === MockPromise.REJECTED) {setTimeout(() => {try {const res = onRejected(this.value)if (res instanceof MockPromise) {res.then(value => {resolve(value)})} else {resolve(res)}} catch (error) {reject(error)}})}/**解决 resolve() 异步处理* 比如 setTimeout(() => { resolve(3) }, 1000)* 这时候 先执行 的是 then 函数,我们可以先把要执行的函数 压入 一个数组中* 等到执行到 resolve 的时候,再去判断状态,然后遍历数组将数组中的函数执行*/if (this.status === MockPromise.PENDING) {// push 的肯定是一个函数this.resolvedTasks.push((value) => {setTimeout(() => {const res = onFulfilled(value)console.log('res===',res );MockPromise.handlePromise(res, resolve, reject)// try {// const res = onFulfilled(value)// if (res instanceof MockPromise) {// res.then(value => {// resolve(value)// })// } else {// resolve(res)// }// } catch (error) {// reject(error)// }})})this.rejectedTasks.push((reason) => {setTimeout(() => {try {const res = onRejected(reason)if (res instanceof MockPromise) {res.then(value => {resolve(value)})} else {resolve(res)}} catch (error) {reject(error)}})})}})// if (this.status === MockPromise.FULFILLED) {// setTimeout(() => {// onFulfilled(this.value)// })// }// if (this.status === MockPromise.REJECTED) {// setTimeout(() => {// onRejected(this.value)// })// }// /**解决 resolve() 异步处理// * 比如 setTimeout(() => { resolve(3) }, 1000)// * 这时候 先执行 的是 then 函数,我们可以先把要执行的函数 压入 一个数组中// * 等到执行到 resolve 的时候,再去判断状态,然后遍历数组将数组中的函数执行// */// if (this.status === MockPromise.PENDING) {// // push 的肯定是一个函数// this.resolvedTasks.push((value) => {// setTimeout(() => {// const res = onFulfilled(value)// console.log('res---', res)// })// })// this.rejectedTasks.push((reason) => {// setTimeout(() => {// onRejected(reason)// })// })// }}}MockPromise.PENDING = 'pending'MockPromise.FULFILLED = 'fulfilled'MockPromise.REJECTED = 'rejected'MockPromise.handlePromise = function (res, resolve, reject) {try {if (res instanceof MockPromise) {res.then(value => {resolve(value)})} else {resolve(res)}} catch (error) {reject(error)}}const dd = new MockPromise((resolve, reject) => {console.log('1');setTimeout(() => {resolve(3)}, 200)}).then((val) => {console.log('val--', val);// return 'haha' + valreturn new MockPromise((resolve, reject) => {resolve('新的promise' + val)})}, reason => {console.log('reason--', reason);}).then(val => {console.log('then--', val);})console.log('2');
