当前位置:网站首页>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
边栏推荐
- [detailed explanation of Huawei machine test] character statistics and rearrangement
- Loop invariant
- 美国费城发生“安全事故” 2名警察遭枪杀
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- useMemo,memo,useRef等相关hooks详解
- freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
- Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
- Isn't it right to put money into the external market? How can we ensure safety?
- Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
- TS所有dom元素的类型声明
猜你喜欢

【华为机试真题详解】字符统计及重排

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

Topology可视化绘图引擎

一键更改多个文件名字

leetcode:881. lifeboat

Introduction, installation, introduction and detailed introduction to postman!

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation

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

Pointer operation - C language
随机推荐
FR练习题目---简单题
Topology visual drawing engine
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
一网打尽异步神器CompletableFuture
Webrtc learning (II)
Thymeleaf 常用函数
Explain Vue's plan to clean up keepalive cache in time
强联通分量
乌卡时代下,企业供应链管理体系的应对策略
03_ Dataimport of Solr
不相交集
Share 20 strange JS expressions and see how many correct answers you can get
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
Section - left closed right open
Fonctions communes de thymeleaf
js亮瞎你眼的日期选择器
04_ Use of solrj7.3 of solr7.3