当前位置:网站首页>手写promise与async await
手写promise与async await
2022-07-05 14:32:00 【原谅我不够洒脱】
promise
先看看promise实现的效果
const Pro = new Promise((resolve,reject) => {
setTimeout(() => {
resolve(123)
}, 5000)
})
Pro.then((res) => {
console.log(res,'----')
})
// 运行代码5秒后控制台打印 123 "----"
自己实现
const PromiseFater = function(callBack) {
this.callBack = callBack
this.then = (back) => {
this.callBack(back)
}
}
const p = new PromiseFater((resolve, reject) => {
console.log(1)
setTimeout(() => {
resolve(123)
}, 5000)
})
p.then((res) => {
console.log(res, '---')
})
// 运行代码5秒后控制台打印 123 "---"
上面只是换了个方法阐述了promise的实现原理,
下面我们加上reject
这代码并不支持连续的.then().then(),只是分析了一下.then().catch()如何实现的,不想把代码搞得太多了,这样不方便看
class MyPromise {
constructor (callBack) {
if (typeof callBack !== 'function') {
throw new Error('必须传入一个函数')
}
// 添加状态
this.status = 'PENDING'
this.thenFun = [] // 存放.then传入的back方法
this.catchFun = [] // 存放.catch传入的back方法
try {
callBack(this.resolve, this.reject)
} catch (err) {
this.reject(err)
}
}
reject = (val) => {
this.status = 'fail'
this.catchFun(val)
}
resolve = (val) => {
this.status = 'success'
this.thenFun(val)
}
then = (back) => {
if (typeof back !== 'function') {
throw new Error('then参数必须传入一个函数')
}
this.thenFun = back
return this
}
catch = (back) => {
if (typeof back !== 'function') {
throw new Error('catch参数必须传入一个函数')
}
this.catchFun = back
return this
}
}
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
reject('失败测试')
}, 2000);
})
promise
.then((res) => {
console.log(res)
})
.catch((res) => {
console.log(res)
})
// 运行,控制台上打印 失败测试
如果把
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
reject('失败测试')
}, 2000);
})
换成
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve('成功测试')
}, 2000);
})
// 运行,控制台上打印 成功测试
如果想实现连续的.then().then(),可以将上面的thenFun 改成thenArr来保存每个then传入的方法,通过状态一次调用,但是何必呢,这样不说写着不好看,用async await他不香嘛
边栏推荐
- 黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- Enjoy what you want. Zhichuang future
- 【学习笔记】图的连通性与回路
- How to choose the appropriate certificate brand when applying for code signing certificate?
- C语言中限定符的作用
- 微帧科技荣获全球云计算大会“云鼎奖”!
- Introduction, installation, introduction and detailed introduction to postman!
- 外盘入金都不是对公转吗,那怎么保障安全?
- R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
猜你喜欢
无密码身份验证如何保障用户隐私安全?
安装配置Jenkins
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
世界环境日 | 周大福用心服务推动减碳环保
LeetCode_ 2 (add two numbers)
How to deeply understand the design idea of "finite state machine"?
Sharing the 12 most commonly used regular expressions can solve most of your problems
Pointer operation - C language
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
Security analysis of Web Architecture
随机推荐
【招聘岗位】软件工程师(全栈)- 公共安全方向
03_ Dataimport of Solr
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
非技术部门,如何参与 DevOps?
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
Use the word "new" to attract curious people
Thymeleaf th:with use of local variables
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
【NVMe2.0b 14-9】NVMe SR-IOV
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
世界环境日 | 周大福用心服务推动减碳环保
微帧科技荣获全球云计算大会“云鼎奖”!
无密码身份验证如何保障用户隐私安全?
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
实现一个博客系统----使用模板引擎技术
R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
Discussion on memset assignment
leetcode:881. lifeboat