首页 技术 正文
技术 2022年11月17日
0 收藏 923 点赞 2,819 浏览 1992 个字
 const PENDING = 'pending',
FULFILLED = 'fulfilled',
REJECTED = 'rejected' class MyPromise {
constructor(executor) {
this.state = PENDING
this.value = undefined
this.reason = undefined
this.onResolvedCallbacks = []
this.onRejectedCallbacks = [] let resolve = (value) => {
if (this.state === PENDING) {
this.state = FULFILLED
this.value = value
this.onResolvedCallbacks.forEach((fn) => fn())
}
} let reject = (reason) => {
if (this.state === PENDING) {
this.state = REJECTED
this.reason = reason
this.onRejectedCallbacks.forEach((fn) => fn())
}
}
try {
executor(resolve, reject)
} catch (err) {
reject(err)
}
} then(onFulFilled, onRejected) {
let p2 = new MyPromise((resolve, reject) => {
let x
if (this.state === FULFILLED) {
setTimeout(() => {
x = onFulFilled(this.value)
//resolve(x); resolvePromise(p2, x, resolve, reject) // x 决定 了 p2 的状态, resolve(x)或者 reject(x);
}, 0)
} if (this.state === REJECTED) {
x = onRejected(this.reason)
resolvePromise(p2, x, resolve, reject)
} if (this.state === PENDING) {
this.onResolvedCallbacks.push(() => {
x = onFulFilled(this.value)
resolvePromise(p2, x, resolve, reject)
})
this.onRejectedCallbacks.push(() => {
x = onRejected(this.reason)
resolvePromise(p2, x, resolve, reject)
})
}
}) return p2
}
} function resolvePromise(p2, x, resolve, reject) {
if (p2 === x) {
return new Error('引用错误')
}
// thenable 对象; blueBird q;
if ((typeof x === 'object' && x !== null) || typeof x === 'function') {
try {
let then = x.then
if (typeof then === 'function') {
then.call(
x,
(y) => {
resolvePromise(p2, y, resolve, reject)
},
(err) => {
reject(err)
}
)
}
} catch (err) {
reject(err)
}
} else {
resolve(x)
}
}
function test() {
return new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve(100)
}, 1000)
})
} var p1 = test()
// p1 => resolve(100) => p1.then(res) res => 100;
// p2 => resolve(res) => p2.then(res) => res ?? var p2 = p1.then(
(res) => {
// return {
// then(resolve, reject) {
// reject(100000);
// }
// };
return new MyPromise((resolve, reject) => {
resolve(
new MyPromise((resolve, reject) => {
resolve(
new MyPromise((resolve, reject) => {
resolve(100000000000)
})
)
})
)
})
},
(err) => console.log(err)
) p2.then(
(res) => {
console.log(res)
},
(err) => {
console.log(err)
}
)

  总结:实现源码需要解决的问题

      promise 源码~     1. 异步的问题;    2. 链式调用 => return this ?    3.在初始化之前调用值的?     4. 递归的问题;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919