当前位置:网站首页>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
边栏推荐
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
- 启牛学堂班主任给的证券账户安全吗?能开户吗?
- 最长公共子序列 - 动态规划
- Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
- 快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
- 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
- TS所有dom元素的类型声明
- Total amount analysis accounting method and potential method - allocation analysis
- Topology可视化绘图引擎
- 【NVMe2.0b 14-9】NVMe SR-IOV
猜你喜欢

CPU设计相关笔记

周大福践行「百周年承诺」,真诚服务推动绿色环保

Thymeleaf 模板的创建与使用

Niuke: intercepting missiles

Section - left closed right open

【学习笔记】阶段测试1

freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向

SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!

选择排序和冒泡排序
随机推荐
启牛证券账户怎么开通,开户安全吗?
Longest common subsequence dynamic programming
循环不变式
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
FR练习题目---综合题
长列表优化虚拟滚动
Penetration testing methodology
Thymeleaf common functions
在Pytorch中使用Tensorboard可视化训练过程
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
mysql8.0JSON_CONTAINS的使用说明
04_ Use of solrj7.3 of solr7.3
Mysql database installation tutorial under Linux
【华为机试真题详解】字符统计及重排
详解Vue适时清理keepalive缓存方案
Explain Vue's plan to clean up keepalive cache in time
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力