function runAsyncTask(callback) {
// queueMicroTask() -> MutationObserver -> setTimeout()
if (typeof queueMicrotask === 'function') {
queueMicrotask(callback)
} else if (typeof MutationObserver === 'function') {
const mutationObserver = new MutationObserver(callback)
const oDiv = document.createElement('div')
mutationObserver.observe(oDiv, { childList: true })
oDiv.innerText = 'longge666'
} else {
setTimeout(callback, 0)
}
}
// Promise构造函数
// Promise状态 status pending fulfilled rejected
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class Promise {
// 实例属性
status = PENDING
result = undefined
// 私有属性 this.#handlers = []
#handles = []
constructor(executor) {
// console.log(executor)
// console.log('构造函数执行了')
const resolve = (result) => {
if (this.status === PENDING) {
this.status = FULFILLED
this.result = result
// 把之前存的函数依次取出来调用
this.#handles.forEach(({ onFulfilled }) => {
onFulfilled()
})
}
}
const reject = (result) => {
if (this.status === PENDING) {
this.status = REJECTED
this.result = result
// 把之前存的函数依次取出来调用
this.#handles.forEach(({ onRejected }) => {
onRejected()
})
}
}
executor(resolve, reject)
}
then(onFulfilled, onRejected) {
onFulfilled =
typeof onFulfilled === 'function' ? onFulfilled : (x) => x
onRejected =
typeof onRejected === 'function'
? onRejected
: (x) => {
throw x
}
const p2 = new Promise((resolve, reject) => {
// 判断状态
if (this.status === FULFILLED) {
runAsyncTask(() => {
try {
const x = onFulfilled(this.result)
if (x instanceof Promise) {
// 说明返回值是一个promise的实例
x.then(
(data) => {
resolve(data)
},
(err) => {
reject(err)
}
)
} else {
// 普通值的时候
resolve(x)
}
} catch (err) {
reject(err)
}
})
} else if (this.status === REJECTED) {
runAsyncTask(() => {
onRejected(this.result)
})
} else {
// 调用then的时候,状态是默认状态,把传入的两个函数存储起来
// [{onFulfilled, onRejected},{OnFulfilled, onRejected}]
this.#handles.push({
onFulfilled: () => {
runAsyncTask(() => {
onFulfilled(this.result)
})
},
onRejected: () => {
runAsyncTask(() => {
onRejected(this.result)
})
},
})
}
})
return p2
}
}
/**/
const p = new Promise((resolve, reject) => {
resolve(1)
})
// then可以调用多次
const p2 = p.then(
(data) => {
console.log(data)
//return 'hello' // ->返回的是普通值,走then的成功回调
//throw 123 // -> 出错 走then的失败回调
return new Promise((resolve, reject) => {
reject(11111)
})
},
(reason) => {
console.log(reason)
}
)
p2.then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
}
)
手撕Promise-then回调循环引用 (1)
function runAsyncTask(callback) {
// queueMicroTask() -> MutationObserver -> setTimeout()
if (typeof queueMicrotask === 'function') {
queueMicrotask(callback)
} else if (typeof MutationObserver === 'function') {
const mutationObserver = new MutationObserver(callback)
const oDiv = document.createElement('div')
mutationObserver.observe(oDiv, { childList: true })
oDiv.innerText = 'longge666'
} else {
setTimeout(callback, 0)
}
}
function resolvePromise(p2, x, resolve, reject) {
// 循环引用
if (x === p2) {
throw new TypeError('Chaining cycle detected for promise #<Promise>')
}
if (x instanceof Promise) {
// 说明返回值是一个promise的实例
x.then(
(data) => {
resolve(data)
},
(err) => {
reject(err)
}
)
} else {
// 普通值的时候
resolve(x)
}
}
// Promise构造函数
// Promise状态 status pending fulfilled rejected
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class Promise {
// 实例属性
status = PENDING
result = undefined
// 私有属性 this.#handlers = []
#handles = []
constructor(executor) {
// console.log(executor)
// console.log('构造函数执行了')
const resolve = (result) => {
if (this.status === PENDING) {
this.status = FULFILLED
this.result = result
// 把之前存的函数依次取出来调用
this.#handles.forEach(({ onFulfilled }) => {
onFulfilled()
})
}
}
const reject = (result) => {
if (this.status === PENDING) {
this.status = REJECTED
this.result = result
// 把之前存的函数依次取出来调用
this.#handles.forEach(({ onRejected }) => {
onRejected()
})
}
}
executor(resolve, reject)
}
then(onFulfilled, onRejected) {
onFulfilled =
typeof onFulfilled === 'function' ? onFulfilled : (x) => x
onRejected =
typeof onRejected === 'function'
? onRejected
: (x) => {
throw x
}
const p2 = new Promise((resolve, reject) => {
// 判断状态
if (this.status === FULFILLED) {
runAsyncTask(() => {
try {
const x = onFulfilled(this.result)
resolvePromise(p2, x, resolve, reject)
} catch (err) {
reject(err)
}
})
} else if (this.status === REJECTED) {
runAsyncTask(() => {
try {
const x = onRejected(this.result)
resolvePromise(p2, x, resolve, reject)
} catch (err) {
reject(err)
}
})
} else {
// 调用then的时候,状态是默认状态,把传入的两个函数存储起来
// [{onFulfilled, onRejected},{OnFulfilled, onRejected}]
this.#handles.push({
onFulfilled: () => {
runAsyncTask(() => {
try {
const x = onFulfilled(this.result)
resolvePromise(p2, x, resolve, reject)
} catch (err) {
reject(err)
}
})
},
onRejected: () => {
runAsyncTask(() => {
try {
const x = onRejected(this.result)
resolvePromise(p2, x, resolve, reject)
} catch (err) {
reject(err)
}
})
},
})
}
})
return p2
}
}
/**/
const p = new Promise((resolve, reject) => {
// resolve(1)
// reject('error')
setTimeout(() => {
reject(1)
}, 1000)
})
// then可以调用多次
const p2 = p.then(
(data) => {
console.log(data)
return 100
},
(reason) => {
console.log(reason)
// return 100
// throw 123
return new Promise((resolve, reject) => {
reject('error')
})
}
)
p2.then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
// throw err
}
)
// catch all ...
// promise a+规范
function runAsyncTask(callback) {
// queueMicroTask() -> MutationObserver -> setTimeout()
if (typeof queueMicrotask === 'function') {
queueMicrotask(callback)
} else if (typeof MutationObserver === 'function') {
const mutationObserver = new MutationObserver(callback)
const oDiv = document.createElement('div')
mutationObserver.observe(oDiv, { childList: true })
oDiv.innerText = 'longge666'
} else {
setTimeout(callback, 0)
}
}
function resolvePromise(p2, x, resolve, reject) {
// 循环引用
if (x === p2) {
throw new TypeError('Chaining cycle detected for promise #<Promise>')
}
if (x instanceof Promise) {
// 说明返回值是一个promise的实例
x.then(
(data) => {
resolve(data)
},
(err) => {
reject(err)
}
)
} else {
// 普通值的时候
resolve(x)
}
}
// Promise构造函数
// Promise状态 status pending fulfilled rejected
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class Promise {
// 实例属性
status = PENDING
result = undefined
// 私有属性 this.#handlers = []
#handles = []
constructor(executor) {
// console.log(executor)
// console.log('构造函数执行了')
const resolve = (result) => {
if (this.status === PENDING) {
this.status = FULFILLED
this.result = result
// 把之前存的函数依次取出来调用
this.#handles.forEach(({ onFulfilled }) => {
onFulfilled()
})
}
}
const reject = (result) => {
if (this.status === PENDING) {
this.status = REJECTED
this.result = result
// 把之前存的函数依次取出来调用
this.#handles.forEach(({ onRejected }) => {
onRejected()
})
}
}
executor(resolve, reject)
}
then(onFulfilled, onRejected) {
onFulfilled =
typeof onFulfilled === 'function' ? onFulfilled : (x) => x
onRejected =
typeof onRejected === 'function'
? onRejected
: (x) => {
throw x
}
const p2 = new Promise((resolve, reject) => {
// 判断状态
if (this.status === FULFILLED) {
runAsyncTask(() => {
try {
const x = onFulfilled(this.result)
resolvePromise(p2, x, resolve, reject)
} catch (err) {
reject(err)
}
})
} else if (this.status === REJECTED) {
runAsyncTask(() => {
try {
const x = onRejected(this.result)
resolvePromise(p2, x, resolve, reject)
} catch (err) {
reject(err)
}
})
} else {
// 调用then的时候,状态是默认状态,把传入的两个函数存储起来
// [{onFulfilled, onRejected},{OnFulfilled, onRejected}]
this.#handles.push({
onFulfilled: () => {
runAsyncTask(() => {
onFulfilled(this.result)
})
},
onRejected: () => {
runAsyncTask(() => {
onRejected(this.result)
})
},
})
}
})
return p2
}
}
/**/
const p = new Promise((resolve, reject) => {
// resolve(1)
// reject('error')
})
// then可以调用多次
const p2 = p.then(
(data) => {
console.log(data)
return 100
},
(reason) => {
console.log(reason)
// return 100
// throw 123
return new Promise((resolve, reject) => {
reject('error')
})
}
)
p2.then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
// throw err
}
)
// promise a+规范