const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECT = 'rejected';
class MyPromise {
#state = PENDING
#result = undefined
#handler = []
constructor(executor){
const resolve = (data) => {
this.#changeState(FULFILLED, data)
}
const reject = (err) => {
this.#changeState(REJECT, err)
}
try{
executor(resolve, reject)
}
catch(err){
reject(err)
}
}
#changeState(state, result){
if(this.#state !== PENDING) return
this.#state = state
this.#result = result
this.#run()
}
// 判断这个callback是不是一个promise
#isPromiseLike(value){
return typeof value == 'object' && typeof value.then == 'function'
// return value instanceof MyPromise || value[Symbol.for('nodejs.runtime.asm.promise')] instanceof MyPromise || value[Symbol.for('nodejs.runtime.asm.promise.constructor')] instanceof MyPromise;
}
// 手动将函数添加到微任务
#runMicroTask(fun){
if (typeof MutationObserver !== 'undefined') {
const textNode = document.createTextNode('0')
const ob = new MutationObserver(fun)
ob.observe(textNode, { characterData: true })
textNode.data = '1'
} else {
process.nextTick(fun)
}
}
#runCall(callBack, resolve, reject){
// then中的回调需要放在微队列中
this.#runMicroTask(() => {
// console.log('www',callBack, resolve, reject)
if (typeof callBack === "function") {
try{
const data = callBack(this.#result);
// 判断这个data返回的是不是一个promise
if(this.#isPromiseLike(data)){
data.then(resolve, reject)
} else resolve(data)
}
catch{
reject(this.#result)
}
} else {
const settled = this.#state === FULFILLED ? resolve : reject
settled(this.#result) //链式回调穿透
}
})
}
#run(){
if (this.#state === PENDING) return
while(this.#handler.length){
const {onFulfilled, onRejected, resolve, reject} = this.#handler.shift()
if (this.#state === FULFILLED) {
this.#runCall(onFulfilled, resolve, reject)
} else {
this.#runCall(onRejected, resolve, reject)
}
}
}
then(onFulfilled, onRejected){
return new MyPromise((resolve,reject) => {
this.#handler.push({
onFulfilled,
onRejected,
resolve,
reject
})
this.#run() //recursion to avoid stack overflow.
})
}
}
、
执行:
const p = new MyPromise((resolve,reject) => {
resolve(1)
}) //constructor for a promise
p.then((val) => {
console.log('data1',val)
return 123
}).then((data) => {
console.log('data2',data)
})
输出:
[Running] node "d:\console\utils\myPromise.js"
data1 1
data2 123
标签:resolve,const,函数,.#,state,promise,reject,手写,data From: https://www.cnblogs.com/xuhuang/p/17391807.html