当前位置:网站首页>Handwriting promise and async await
Handwriting promise and async await
2022-07-05 14:36:00 【Forgive me for not being free and easy enough】
promise
Have a look first promise To achieve the effect of
const Pro = new Promise((resolve,reject) => {
setTimeout(() => {
resolve(123)
}, 5000)
})
Pro.then((res) => {
console.log(res,'----')
})
// Run code 5 Seconds later, the console prints 123 "----"
Realize it by yourself
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, '---')
})
// Run code 5 Seconds later, the console prints 123 "---"
The above is just another way to explain promise Implementation principle of ,
Let's add reject
This code does not support continuous .then().then(), Just analyzed .then().catch() How to achieve it , Don't want to make too much code , This is inconvenient to see
class MyPromise {
constructor (callBack) {
if (typeof callBack !== 'function') {
throw new Error(' You must pass in a function ')
}
// Add state
this.status = 'PENDING'
this.thenFun = [] // Deposit .then Incoming back Method
this.catchFun = [] // Deposit .catch Incoming back Method
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 Parameters must be passed into a function ')
}
this.thenFun = back
return this
}
catch = (back) => {
if (typeof back !== 'function') {
throw new Error('catch Parameters must be passed into a function ')
}
this.catchFun = back
return this
}
}
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
reject(' Failed tests ')
}, 2000);
})
promise
.then((res) => {
console.log(res)
})
.catch((res) => {
console.log(res)
})
// function , Print on the console Failed tests
If you put
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
reject(' Failed tests ')
}, 2000);
})
Switch to
const promise = new MyPromise((resolve, reject) => {
setTimeout(() => {
resolve(' Successful test ')
}, 2000);
})
// function , Print on the console Successful test
If you want to achieve continuous .then().then(), You can put the above thenFun Change to thenArr To save each then Incoming method , Call once through the state , But why , It's not good to write like this , use async await He doesn't smell good
边栏推荐
- 周大福践行「百周年承诺」,真诚服务推动绿色环保
- The function of qualifier in C language
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- 网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
- 在Pytorch中使用Tensorboard可视化训练过程
- LeetCode_ 3 (longest substring without repeated characters)
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
- 做自媒体视频二次剪辑,怎样剪辑不算侵权
- How to open an account of qiniu securities? Is it safe to open an account?
猜你喜欢

Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性

IPv6与IPv4的区别 网信办等三部推进IPv6规模部署

ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器

Loop invariant

Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million

直播预告|如何借助自动化工具落地DevOps(文末福利)

超级哇塞的快排,你值得学会!

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
![[learning notes] stage test 1](/img/22/ad16375d8d1510c2ec75c56403a8bf.png)
[learning notes] stage test 1
随机推荐
Section - left closed right open
Un week - end heureux
Disjoint Set
How to make a second clip of our media video without infringement
最长公共子序列 - 动态规划
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
循环不变式
微帧科技荣获全球云计算大会“云鼎奖”!
mysql8.0JSON_ Instructions for using contains
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
注意!软件供应链安全挑战持续升级
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Security analysis of Web Architecture
Pointer operation - C language
Implement a blog system -- using template engine technology
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
Thymeleaf 模板的创建与使用
不相交集
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c