当前位置:网站首页>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
边栏推荐
- The new MySQL table has a self increasing ID of 20 bits. The reason is
- Paging SQL
- 网络安全之渗透靶场实战详解
- Do you really understand the principle of code scanning login?
- 计算饼状图百分比
- Lock lock
- Your brain expands and shrinks over time — these charts show how
- Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
- Zhongang Mining: Fluorite Flotation Process
- Camtasia2022软件新增功能介绍
猜你喜欢

How vscode compares the similarities and differences between two files

Solution architect's small bag - 5 types of architecture diagrams

Node access to Alipay open platform sandbox to achieve payment function

Grpc gateway based on Ocelot

Go language, array, string, slice

MySQL master-slave replication
![[PROJECT] small hat takeout (8)](/img/54/0187eeb637f4dcd4ad3969b00e2b77.png)
[PROJECT] small hat takeout (8)

Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears

扫码登录的原理你真的了解吗?

Nodejs installation and environment configuration
随机推荐
1-1 NodeJS
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
Talk about browser cache control
Dlib of face recognition framework
Production environment——
Pycham connects to the remote server and realizes remote debugging
VsCode-Go
STM32__ 03 - beginner timer
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
Milvus 2.0 質量保障系統詳解
DDT + Excel for interface test
Sub database and sub table & shardingsphere
[registration] tf54: engineer growth map and excellent R & D organization building
Get the column name list of the table quickly in Oracle
MySQL master-slave synchronization pit avoidance version tutorial
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
Mock test using postman
Paging SQL
Redis docker installation
Shell script -- shell programming specification and variables