const PENDING = 'pending' const FULFILLED = 'fullfilled' const REJECTED = 'rejected' class MyPromise { state = PENDING result = undefined handlers = [] constructor(executor) { const resolve = (data) => { this.changeState(FULFILLED, data) } const reject = (reson) => { this.changeState(REJECTED, reson) } try { executor(resolve, reject) } catch (error) { reject(error) } } changeState(state, result) { if (this.state !== PENDING) return this.state = state this.result = result this.run() } isPromiseLike = (value) => { if (value !== null && (typeof value === 'object' || typeof value === 'function')) { return typeof value.then === 'function' } return false } runMocroTask = (func) => { if (typeof process === 'object' && typeof process.nextTick === 'function') { process.nextTick(func) } else if (typeof MutationObserver === 'function') { const ob = new MutationObserver(func) const textNode = document.createTextNode('1') ob.observe(textNode, { characterData: true }) textNode.data = '2' } else { setTimeout(func, 0) } } runOne = (callback, resolve, reject) => { this.runMocroTask(() => { // 1、对应回调函数不是一个函数 直接把结果返回 if (typeof callback !== 'function') { const settled = this.state === FULFILLED ? resolve : reject settled(this.result) return } // 2、回调函数是一个函数 try { const data = callback(this.result) // 3、返回的是不是promise(满足promiseA+规范就行) if (this.isPromiseLike(data)) { data.then(resolve, reject) } else { resolve(data) } } catch (error) { reject(error) } }) } run() { if (this.state === PENDING) return while (this.handlers.length) { const { onFulfilled, onRejected, reject, resolve } = this.handlers.shift() if (this.state === FULFILLED) { this.runOne(onFulfilled, resolve, reject) } else { this.runOne(onRejected, resolve, reject) } } } then(onFulfilled, onRejected) { return new MyPromise((resolve, reject) => { this.handlers.push({ onFulfilled, onRejected, resolve, reject }) this.run() }) } } // 测试 function delay(duration = 1000) { return new MyPromise((resolve, reject) => { setTimeout(resolve, duration) }) } async function test() { await delay(1000) console.log('ok1'); await delay(1000) console.log('ok2'); } test() // const p = new MyPromise((resolve, reject) => { // setTimeout(() => { // resolve(1) // }, 1000); // }) // p.then((data) => { // console.log('ok1', data); // return new Promise((resolve, reject) => { // setTimeout(() => { // resolve(data * 2) // }, 1000); // }) // }).then((data) => { // console.log('ok2', data); // })
标签:resolve,const,state,promise,reject,return,手写,data From: https://www.cnblogs.com/duanlibo/p/17393791.html