⼿写 Promise

2020-09-14  本文已影响0人  ZoranLee

实现Promise

const PENDING = 'pending' 
const RESOLVED = 'resolved'
 const REJECTED = 'rejected'
function MyPromise(fn) {
const that = this
that.state = PENDING
that.value = null
that.resolvedCallbacks = []
that.rejectedCallbacks = []
}

resolve、reject

function resolve(value) {
     if (that.state === PENDING) {
         that.state = RESOLVED 
         that.value = value 
         that.resolvedCallbacks.map(cb => cb(that.value)) 
    }
 }

function reject(value) { 
if (that.state === PENDING) {
     that.state = REJECTED 
     that.value = value 
     that.rejectedCallbacks.map(cb => cb(that.value)) 
} 
}

Promise

try { 
fn(resolve, reject)
 } catch (e) {
 reject(e) 
}

then 函数

MyPromise.prototype.then = function(onFulfilled, onRejected) { 
const that = this 
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v onRejected = typeof onRejected === 'function' ? onRejected : r => {
 throw r 
} 
if (that.state === PENDING) { 
that.resolvedCallbacks.push(onFulfilled) that.rejectedCallbacks.push(onRejected)
 }
if (that.state === RESOLVED) {
 onFulfilled(that.value) 
}
if (that.state === REJECTED) { 
onRejected(that.value)
 } 
}
Promise.resolve(4).then().then((value) => console.log(value))
new MyPromise((resolve, reject) => {
 setTimeout(() => {
 resolve(1) }, 0) 
}).then(value => { 
console.log(value) 
})

优化

resolve、reject

function resolve(value) {
 if (value instanceof MyPromise) { 
return value.then(resolve, reject) 
}
setTimeout(() => {
 if (that.state === PENDING) 
{ that.state = RESOLVED
 that.value = value 
that.resolvedCallbacks.map(cb => cb(that.value)) } }, 0) 
}
function reject(value) { 
setTimeout(() => {
 if (that.state === PENDING) {
 that.state = REJECTED 
 that.value = value that.rejectedCallbacks.map(cb => cb(that.value)) } }, 0)
 }

then 函数

if (that.state === PENDING) { 
return (promise2 = new MyPromise((resolve, reject) => { that.resolvedCallbacks.push(() => { 
try { 
const x = onFulfilled(that.value) 
resolutionProcedure(promise2, x, resolve, reject)} catch (r) { 
reject(r)
 } })
that.rejectedCallbacks.push(() => { 
try { 
const x = onRejected(that.value) 
resolutionProcedure(promise2, x, resolve, reject)
} catch (r) {
 reject(r) 
} 
})
 })) }

判断执⾏态的逻辑

if (that.state === RESOLVED) {
 return (promise2 = new MyPromise((resolve, reject) => {
 setTimeout(() => {
 try { 
const x = onFulfilled(that.value) 
resolutionProcedure(promise2, x, resolve, reject)
} catch (reason) { 
reject(reason) }
 }) })) 
}

兼容多种 Promise 的 resolutionProcedure 函数

function resolutionProcedure(promise2, x, resolve, reject) {
 if (promise2 === x) { 
return reject(new TypeError('Error')) 
} 
}
let called = false 
if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
 try { 
let then = x.then 
if (typeof then === 'function') { 
then.call( x,y => { 
if (called) return called = true 
resolutionProcedure(promise2, y, resolve, reject)
 },e => {
 if (called) return called = true 
reject(e) } ) 
} else { 
resolve(x) }
 } catch (e) { 
if (called) return called = true 
reject(e) } } 
else { 
resolve(x) 
}
上一篇 下一篇

猜你喜欢

热点阅读