当前位置:网站首页>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
边栏推荐
- Countermeasures of enterprise supply chain management system in UCA Era
- ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
- [learning notes] connectivity and circuit of graph
- How to protect user privacy without password authentication?
- webRTC SDP mslabel lable
- 在Pytorch中使用Tensorboard可视化训练过程
- 一网打尽异步神器CompletableFuture
- Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
- 裁员下的上海
- MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
猜你喜欢

Pointer operation - C language

如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴

ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
![Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]](/img/c2/a5f5fe17a6bd1f6f9df828ddd224d6.png)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]

Thymeleaf 模板的创建与使用

循环不变式

APR protocol and defense

PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading

Section - left closed right open
随机推荐
Topology可视化绘图引擎
乌卡时代下,企业供应链管理体系的应对策略
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
非技术部门,如何参与 DevOps?
Sharing the 12 most commonly used regular expressions can solve most of your problems
How can non-technical departments participate in Devops?
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
leetcode:881. 救生艇
动态规划
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
无密码身份验证如何保障用户隐私安全?
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
【招聘岗位】软件工程师(全栈)- 公共安全方向
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
Catch all asynchronous artifact completable future
分享 12 个最常用的正则表达式,能解决你大部分问题
LeetCode_ 67 (binary sum)
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
dynamic programming