const resolvePromise = (promise2,x,resolve,reject) => {
if(promise2 === x){
return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
}
let called;
if(typeof x === 'function' || (typeof x === 'object' && typeof x !==null)){
try{
let then = x.then
if(typeof then === 'function'){
then.call(x,y=>{
if(called) return
called = true;
resolvePromise(promise2,y,resolve,reject)
},r=>{
if(called) return
called = true;
reject(r)
})
}else{
resolve(x)
}
}catch(e){
if(called) return
called = true;
reject(e)
}
}else{
resolve(x)
}
}
const PENDING = 'PEDNING'
const FULFILLED = 'FULFILLED'
const REJECTED = 'REJECTED'
class Promise{
constructor(executor){
this.status = PENDING
this.value = undefined
this.reason = undefined
this.onResolvedCallbacks = []
this.onRejectedCallbacks = []
let resove = (value)=>{
if(this.status === PENDING){
this.status = FULFILLED
this.value = value
this.onResolvedCallbacks.forEach(fn=>fn())
}
}
let reject = (reason)=>{
if(this.status === PENDING){
this.status = REJECTED
this.reason = reason
this.onRejectedCallbacks.forEach(fn=>fn())
}
}
try {
executor(resolve,reject)
}catch(e){
reject(e)
}
}
then(onFulfilled,onRejected){
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v;
onRejected = typeof onRejected === 'function' ? onRejected : e => {throw e}
let promise2 = new Promise((resolve,reject)=>{
if(this.status === FUlFILLED){
setTimeout(()=>{
try{
let x = onFulfilled(this.value)
resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
})
}
if(this.status === REJECTED){
setTimeout(()=>{
try{
let x = onRejected(this.reason)
resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
})
}
if(this.status === PENDING){
this.onResolvedCallbacks.push(()=>{
setTimeout(()=>{
try{
let x = onFulfilled(this.value)
resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
})
})
this.onRejectedCallbacks.push(()=>{
setTimeout(()=>{
try{
let x = onRejected(this.reason)
resolvePromise(promise2,x,resolve,reject)
}catch(e){
reject(e)
}
})
})
}
})
return promise2
}
}
手写promise
原创StavinLi ©著作权
文章标签 巩固 文章分类 JavaScript 前端开发
-
TypeScript 中 Promise 深度解析
本文给出Typescript编程中异步对象Promise应用的各种场景,并归纳使用注意事项。
泛型 TypeScript Promise Javascript 链式编程 -
手写 promise
Promises/A+: https://promisesaplus.com/[译]Promise/A+ 规范: https://zhuanlan.zhihu.com/p/143204897
javascript 前端 typescript 加锁 回调函数 -
promise用法总结以及手写promise
Promise.all() 接收一个 Promise 数组,返回一个新的 Promise,当所有的 Promise 都成功时,该 Promise 会成功,返回一
前端 javascript 异步操作 回调函数 数组 -
Promise从入门到手写 | [Promise系列一]
从零开始,带着你入门Promise并亲手实现。本文分为四大部分,包括Promise介绍,Promise特点,Promise使用,和Promise手写~
JavaScript 回调函数 返回结果 异步操作 -
手写Promise实现过程
手写Promise实现过程 1、实现Promise的核心功能 2、判断下当执行器
回调函数 数组 构造器 值传递 静态方法 -
微服务与面向服务编程(SOA)入门指南:从架构演进到 Spring Cloud 实践(初学者友好版)
本文系统介绍了微服务架构的演进过程,从单体架构到垂直架构,再到SOA和微服务架构。重点解析了微服务与SOA的核心区别:微服务粒度更细、采用轻量级通信、服务边界清晰、数据完全独立。文章详细阐述了微服务设计的"单一原则"实践方法,并推荐使用SpringCloudAlibaba框架实现微服务,介绍了Nacos、Sentinel、Seata等核心组件功能。最后为初学者提供了常见误区预防和学习路径建议,强调理解架构演进逻辑比追求完美拆分更重要。
#maven #spring boot #spring cloud #java-ee 微服务 -
【性能监控】一文搞定普罗米修斯+格拉法纳+InfluxDB国内镜像安装与性能监控平台搭建
Docker 镜像拉取失败的多种解决方案使用 DaoCloud、中科大等国内镜像源搭建 JMeter + InfluxDB + Grafana 实时性能监控平台解决 Grafana 连接 InfluxDB 时的网络问题。
#jmeter #prometheus #influxdb #grafana docker -
【文档】搭建 TFTP 服务器
景,支持通过Kickstart实现无人值守安装。
#TFTP linux 服务器 vim
















