当前位置:网站首页>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
边栏推荐
- Shell脚本——Shell编程规范及变量
- Selenium IDE and XPath installation of chrome plug-in
- 聊一聊浏览器缓存控制
- Milvus 2.0 質量保障系統詳解
- Summary of common websites
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
- 无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
- Node access to Alipay open platform sandbox to achieve payment function
- About stream flow, write it down briefly------
- Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
猜你喜欢

oracle 中快速获取表的列名列表

How to build tiktok user trust and drive fan growth

Construction of promtail + Loki + grafana log monitoring system

New project of OMNeT learning

NVIDIA graphics card driver error

Project framework of robot framework
![[pimf] openharmony paper Club - what is the experience of wandering in ACM survey](/img/b6/3df53baafb9aad3024d10cf9b56230.png)
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey

1-1 NodeJS

vscode如何比较两个文件的异同

Shell脚本——Shell编程规范及变量
随机推荐
Path environment variable
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
【题解】[SHOI2012] 随机树
PostgreSQL column storage and row storage
Detailed explanation of Niuke - Gloves
Derivation of Σ GL perspective projection matrix
RTKLIB 2.4.3源码笔记
Website_ Collection
1-2 characteristics of nodejs
True math problems in 1959 college entrance examination
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
SPC introduction
如何建立 TikTok用户信任并拉动粉丝增长
Freecodecamp ---- budget & category exercise
Use case execution of robot framework
Nacos + aspnetcore + Ocelot actual combat code
Document operation II (5000 word summary)
New project of OMNeT learning
Collect blog posts
Copy constructor shallow copy and deep copy