当前位置:网站首页>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
边栏推荐
- Zhongang Mining: Fluorite Flotation Process
- Loggie source code analysis source file module backbone analysis
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- SPC introduction
- ∑GL-透视投影矩阵的推导
- RTKLIB 2.4.3源码笔记
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- Lock lock
- Mock test using postman
- 文件操作《二》(5000字总结篇)
猜你喜欢
ByteVCharts可视化图表库,你想要的我都有
Project framework of robot framework
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
Smart doc + Torna generate interface document
Change the password after installing MySQL in Linux
Use case labeling mechanism of robot framework
File upload and download of robot framework
PyTorch:train模式与eval模式的那些坑
groutine
Bytevcharts visual chart library, I have everything you want
随机推荐
Talk about browser cache control
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
Introduction to how to set up LAN
Camtasia2022软件新增功能介绍
[markdown notes]
Modify the test case name generated by DDT
[registration] tf54: engineer growth map and excellent R & D organization building
文件操作《二》(5000字总结篇)
New keyword learning and summary
关于局域网如何组建介绍
Get the column name list of the table quickly in Oracle
New project of OMNeT learning
_ Mold_ Board_
The new MySQL table has a self increasing ID of 20 bits. The reason is
vscode如何比较两个文件的异同
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
Node access to Alipay open platform sandbox to achieve payment function
Bytevcharts visual chart library, I have everything you want
手写事件发布订阅框架
Paging the list collection