当前位置:网站首页>Promise (I)
Promise (I)
2022-04-23 16:59:00 【Endless cake】
Promise Three states of
1.pending
2.resolved
3.rejected
Three state relationships
pending -> resolved ( Can convert )
pending -> rejected ( Can convert )
<script>
const p1 = new Promise((resolve, reject) => { // establish promise
})
console.log(p1)
// pending=>resolved
const p2 = new Promise((resolve, reject) => { // establish promise2
resolve()
})
console.log(p2)
// pending=>reject
const p3 = new Promise((resolve, reject) => { // establish promise3
reject()
})
console.log(p3)
</script>
Promise Different state performance
//pending In state promise Not trigger then and catch
const p1 = new Promise((resolve, reject) => { // establish promise
})
console.log(p1)
p1.then(()=>{
console.log('p1 then')
}).catch(()=>{
console.log('p1 catch')
})
//resolve In state promise Will trigger then The callback function inside
const p2 = Priomise.resolve() // Abbreviation
p2.then(()=>{
console.log('p2 then')
}).catch(()=>{
console.log('p2 catch')
})
//reject In state promise Will trigger catch The callback function inside
const p3 = Promise reject()
p3.then(()=>{
console.log('p3 then')
}).catch(()=>{
console.log('p3 catch')
})
then、catch Yes Promise The effect of state
const p1 = Promise.resolve()
console.log(p1)
//p1.then There is no exception thrown in the callback function of , therefore , Will return a resolve
const res = p1.then(()=>{
console.log('success')
})
//p1.then An exception is thrown in the callback function of , therefore , Will return a reject
const res = p1.then(()=>{
throw new Error('error')// Here I throw an exception
})
//p1.catch There is no exception thrown in the callback function of , therefore , Will return a resolve
const res = p1.catch(()=>{
console.log('success')
})
//p1.catch An exception is thrown in the callback function of , therefore , Will return a reject
const res = p1.catch(()=>{
throw new Error('error')// Here I throw an exception
})
summary : No matter what then || catch Did you throw an exception ,p.then perhaps p.catch Will return to one resolve State of Promise. If an exception is thrown ,p.then perhaps p.catch Will return to reject State of Promise.
版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554519804.html
边栏推荐
- 1-1 NodeJS
- Derivation of Σ GL perspective projection matrix
- 正则过滤内网地址和网段
- RTKLIB 2.4.3源码笔记
- Mock test using postman
- MySQL master-slave configuration under CentOS
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- Document operation II (5000 word summary)
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
猜你喜欢
vscode如何比较两个文件的异同
groutine
Construction of promtail + Loki + grafana log monitoring system
Milvus 2.0 质量保障系统详解
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
feign报400处理
面试百分百问到的进程,你究竟了解多少
True math problems in 1959 college entrance examination
File upload and download of robot framework
RTKLIB 2.4.3源码笔记
随机推荐
Log4j output log information to file
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
Quick install mongodb
批量制造测试数据的思路,附源码
freeCodeCamp----prob_ Calculator exercise
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
织梦DEDECMS安全设置指南
Use case execution of robot framework
JSON deserialize anonymous array / object
PHP efficiently reads large files and processes data
[registration] tf54: engineer growth map and excellent R & D organization building
STM32__03—初识定时器
Website_ Collection
How to build tiktok user trust and drive fan growth
Shortcut keys (multiline)
杂文 谈谈古典的《拆掉思维里的墙》
Knowledge points and examples of [seven input / output systems]
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
1-1 NodeJS