当前位置:网站首页>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
边栏推荐
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
- Nodejs installation and environment configuration
- 聊一聊浏览器缓存控制
- 如何用Redis实现分布式锁?
- ◰ GL shadow map core steps
- Talk about browser cache control
- [PROJECT] small hat takeout (8)
- Milvus 2.0 détails du système d'assurance de la qualité
- Dlib of face recognition framework
- Shell script -- shell programming specification and variables
猜你喜欢
The new MySQL table has a self increasing ID of 20 bits. The reason is
SQL database
Selenium IDE and XPath installation of chrome plug-in
Knowledge points and examples of [seven input / output systems]
TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
Do you really understand the principle of code scanning login?
Document operation II (5000 word summary)
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Pycham connects to the remote server and realizes remote debugging
Derivation of Σ GL perspective projection matrix
随机推荐
Go language, array, string, slice
Project framework of robot framework
Your brain expands and shrinks over time — these charts show how
New keyword learning and summary
Log4j output log information to file
Interface document yaml
How much do you know about the process of the interview
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
Use case execution of robot framework
PostgreSQL列存与行存
groutine
【题解】[SHOI2012] 随机树
Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
详解牛客----手套
Change the password after installing MySQL in Linux
Detailed explanation of Niuke - Gloves
手写事件发布订阅框架
Document operation II (5000 word summary)
Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition
MySQL master-slave replication