promise 实现
1实现异步调用
const PENDDING = 'pendding',
RESOLVED = 'resolved',
REJECTED = 'rejected'
class myPromise {
constructor(handler){
this.status = PENDDING
this.value = undefined
this.resolveCallback = []
this.rejectCallback = []
let resolve = (value)=>{
if(this.status === PENDDING){
this.status = RESOLVED
this.value = value
this.resolveCallback.forEach((cb)=>{
cb()
})
}
}
let reject = (error)=>{
if(this.status === PENDDING){
this.status = REJECTED
this.reason = error
this.rejectCallback.forEach((cb)=>{
cb()
})
}
}
try{
handler(resolve,reject)
}catch{
reject(error)
}
}
then(onResolve=val=>val,onReject){
if(this.status === PENDDING){
this.resolveCallback.push(()=>onResolve(this.value))
}
if(this.status === RESOLVED){
onResolve(this.value)
}
if(this.status === REJECTED){
onReject(this.reason)
}
}
}
new myPromise((resolve)=>{
setTimeout(()=>{
resolve(123)
},2000)
}).then((res)=>{
console.log(res)
})
2.实现 链式调用并返回普通值 主要是promiseResultProduce这个函数
const PENDDING = 'pendding',
RESOLVED = 'resolved',
REJECTED = 'rejected'
class myPromise {
constructor(handler){
this.status = PENDDING
this.value = undefined
this.resolveCallback = []
this.rejectCallback = []
let resolve = (value)=>{
if(this.status === PENDDING){
this.status = RESOLVED
this.value = value
this.resolveCallback.forEach((cb)=>{
cb()
})
}
}
let reject = (error)=>{
if(this.status === PENDDING){
this.status = REJECTED
this.reason = error
this.rejectCallback.forEach((cb)=>{
cb()
})
}
}
try{
handler(resolve,reject)
}catch{
reject(error)
}
}
promiseResultProduce(promise2,val,resolve,reject){
if(val instanceof myPromise){
}else if((typeof val === 'object' || typeof val === 'function')){
if(typeof val.then === 'function'){
}else{
resolve(val)
}
}else{
resolve(val)
}
}
then(onResolve=val=>val,onReject){
if(this.status === PENDDING){
const promise2 = new myPromise((resolve,reject)=>{
this.resolveCallback.push(()=>{
let val = onResolve(this.value)
setTimeout(()=>{
this.promiseResultProduce(promise2,val,resolve,reject)
},0)
})
})
return promise2
}
if(this.status === RESOLVED){
const promise2 = new myPromise((resolve,reject)=>{
let val = onResolve(this.value)
setTimeout(()=>{
this.promiseResultProduce(promise2,val,resolve,reject)
},0)
})
return promise2
}
if(this.status === REJECTED){
onReject(this.reason)
}
}
}
new myPromise((resolve)=>{
setTimeout(()=>{
resolve(123)
},2000)
}).then((res)=>res)
.then((res)=>{
console.log(res)
return res
}).then((res)=>{
console.log(res)
})
3 实现 支持promise对象
const PENDDING = 'pendding',
RESOLVED = 'resolved',
REJECTED = 'rejected'
class myPromise {
constructor(handler){
this.status = PENDDING
this.value = undefined
this.resolveCallback = []
this.rejectCallback = []
let resolve = (value)=>{
if(this.status === PENDDING){
this.status = RESOLVED
this.value = value
this.resolveCallback.forEach((cb)=>{
cb()
})
}
}
let reject = (error)=>{
if(this.status === PENDDING){
this.status = REJECTED
this.reason = error
this.rejectCallback.forEach((cb)=>{
cb()
})
}
}
try{
handler(resolve,reject)
}catch{
reject(error)
}
}
promiseResultProduce(promise2,val,resolve,reject){
if(val instanceof myPromise){
if(val.status === PENDDING){
console.log(222)
val.then(y=>{
console.log(y)
this.promiseResultProduce(promise2,y,resolve,reject)
},reject)
}else{
/*注意此时val是promise自然有this.value,this.status,this.reason属性*/
val.status === RESOLVED && resolve(val.value)
val.status === REJECTED && reject(val.reason)
}
} else if((typeof val === 'object' || typeof val === 'function')){
} else{
console.log(1111)
resolve(val)
}
}
then(onResolve=val=>val,onReject){
if(this.status === PENDDING){
const promise2 = new myPromise((resolve,reject)=>{
this.resolveCallback.push(()=>{
let val = onResolve(this.value)
setTimeout(()=>{
this.promiseResultProduce(promise2,val,resolve,reject)
},0)
})
})
return promise2
}
if(this.status === RESOLVED){
const promise2 = new myPromise((resolve,reject)=>{
let val = onResolve(this.value)
this.resolveCallback.push(()=>{
setTimeout(()=>{
this.promiseResultProduce(promise2,val,resolve,reject)
},0)
})
})
return promise2
}
if(this.status === REJECTED){
onReject(this.reason)
}
}
}
new myPromise((resolve)=>{
setTimeout(()=>{
resolve(123)
},2000)
})
.then((res)=>{
console.log(res)
return new myPromise((resolve,reject)=>{
setTimeout(()=>{
resolve(666)
},2000)
})
})
.then((res)=>{
console.log(res)
})
4支持thenable对象
const PENDDING = 'pendding',
RESOLVED = 'resolved',
REJECTED = 'rejected'
class myPromise {
constructor(handler){
this.status = PENDDING
this.value = undefined
this.resolveCallback = []
this.rejectCallback = []
let resolve = (value)=>{
if(this.status === PENDDING){
this.status = RESOLVED
this.value = value
this.resolveCallback.forEach((cb)=>{
cb()
})
}
}
let reject = (error)=>{
if(this.status === PENDDING){
this.status = REJECTED
this.reason = error
this.rejectCallback.forEach((cb)=>{
cb()
})
}
}
try{
handler(resolve,reject)
}catch{
reject(error)
}
}
promiseResultProduce(promise2,val,resolve,reject){
if(val instanceof myPromise){
if(val.status === PENDDING){
val.then(y=>{
this.promiseResultProduce(promise2,y,resolve,reject)
},reject)
}else{
/*注意此时val是promise自然有this.value,this.status,this.reason属性*/
val.status === RESOLVED && resolve(val.value)
val.status === REJECTED && reject(val.reason)
}
} else if((typeof val === 'object' || typeof val === 'function')){
if(typeof val.then === 'function'){
val.then(y=>{
this.promiseResultProduce(promise2,y,resolve,reject)
},reject)
}else{
resolve(val)
}
} else{
resolve(val)
}
}
then(onResolve=val=>val,onReject){
if(this.status === PENDDING){
const promise2 = new myPromise((resolve,reject)=>{
this.resolveCallback.push(()=>{
let val = onResolve(this.value)
setTimeout(()=>{
this.promiseResultProduce(promise2,val,resolve,reject)
},0)
})
})
return promise2
}
if(this.status === RESOLVED){
const promise2 = new myPromise((resolve,reject)=>{
let val = onResolve(this.value)
this.resolveCallback.push(()=>{
setTimeout(()=>{
this.promiseResultProduce(promise2,val,resolve,reject)
},0)
})
})
return promise2
}
if(this.status === REJECTED){
onReject(this.reason)
}
}
}
// 支持thenable对象
// let result = {
// then(r){
// r(444)
// }
// }
// result.then(y=>{
// console.log(y)
// })
/*穿透的原理*/
// new myPromise((resolve)=>{
// setTimeout(()=>{
// resolve(123)
// },2000)
// }).then((res)=>res).then((res)=>{
// console.log(res)
// })
Promise.allSettled = function(param){
let result = []
let count = 0
let ln = param.length
return new Promise((resolve,reject)=>{
for(let task of param){
Promise.resolve(task).then((res)=>{
result[count] = {value:res,status:'fulfilled'};
++count === ln && resolve(result)
}).catch((err)=>{
result[count] = {reason:err,status:'rejected'};
++count === ln && resolve(result)
})
}
})
}
const test2 = 'test2'
const test3 = ()=>{
return new Promise((resolve,reject)=>{
setTimeout(()=>{
reject('err')
},1000)
})
}
Promise.allSettled([test2,test3()]).then((res)=>{
console.log(res)
})
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
· 重磅发布!DeepSeek 微调秘籍揭秘,一键解锁升级版全家桶,AI 玩家必备神器!
2020-06-02 element跳转至最后一行和第一行