当前位置:网站首页>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
边栏推荐
- 【招聘岗位】基础设施软件开发人员
- 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
- PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
- Topology visual drawing engine
- 【華為機試真題詳解】歡樂的周末
- The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
- 通过npm 或者 yarn安装依赖时 报错 出现乱码解决方式
- mysql8.0JSON_CONTAINS的使用说明
- 04_ Use of solrj7.3 of solr7.3
- Sharing the 12 most commonly used regular expressions can solve most of your problems
猜你喜欢
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
用 Go 跑的更快:使用 Golang 为机器学习服务
【NVMe2.0b 14-9】NVMe SR-IOV
面试突击62:group by 有哪些注意事项?
周大福践行「百周年承诺」,真诚服务推动绿色环保
Security analysis of Web Architecture
超级哇塞的快排,你值得学会!
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
FR练习题目---简单题
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
随机推荐
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
用 Go 跑的更快:使用 Golang 为机器学习服务
CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
PostgreSQL 13 installation
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
03_ Dataimport of Solr
leetcode:881. 救生艇
[learning notes] stage test 1
选择排序和冒泡排序
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
anaconda使用中科大源
[detailed explanation of Huawei machine test] character statistics and rearrangement
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Two policemen were shot dead in a "safety accident" in Philadelphia, USA
Total amount analysis accounting method and potential method - allocation analysis
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
【華為機試真題詳解】歡樂的周末
两个BI开发,3000多张报表?如何做的到?
申请代码签名证书时如何选择合适的证书品牌?