当前位置:网站首页>Promise (IV)
Promise (IV)
2022-04-23 17:03:00 【Endless cake】
Promise Several key issues
1. If the current Promise Status as pending state , After throwing an exception ,Promise Will become rejected state
const p6 = new Promise((resolve, reject) => {
//resolve(1) //promise Turn into resolved Success status
// reject(2) //promise Turn into rejected Failure status
throw 3 // Throw an exception ,Promise Turn into rejected state ,reason For the thrown error
})
p6.then(() => {
value => {},
reason => {
console.log('reason',reason)
}
})
2. One Promise More than one success was specified / Failed callback function , Are they called ?
// Multiple calls will all call back
p6.then(() => {
value => {
console.log('success',value)
},
reason => {
console.log('reason',reason)
}
})
p6.then(() => {
value => {
console.log('success',value)
},
reason => {
console.log('reason',reason)
}
})
3. change Promise State and specify the callback function who comes first and then ?
// routine : First specify the callback function , Changed state after
new Promise((resolve, reject) => {
setTimeout(() => { // Changed state after ( Also specify the data , Asynchronously executing callback functions )
resolve(1)
})
}).then(() => { // First specify the callback function , Saves the currently specified callback function
value => {},
reason => {
console.log('reason', reason)
}
})
// Method 1: Change the state first , In the specified callback function
new Promise((resolve, reject) => {
resolve(1) // Change your state first ( Also specify the data )
}).then(() => { // After the specified callback function , Asynchronously executing callback functions
value => {
console.log('value', value)
},
reason => {
console.log('reason', reason)
}
})
// Method 2: Change the state first , In the specified callback function
const ps = new Promise((resolve, reject) => {
setTimeout(() => { // Changed state after ( Also specify the data , Asynchronously executing callback functions )
resolve(1)
},1000)
})
setTimeout(()=>{
p.then(() => { // First specify the callback function , Saves the currently specified callback function
value => {},
reason => {
console.log('reason', reason)
}
})
},1000)
Be careful : stay Promise in , Successful and failed callback functions , Always asynchronous execution . Even if the conditions are met , Not immediately .
4.Promise.then Returned new Promise What determines the outcome status of ?
//(1) Simple expression : from then() Returned new Promise The result of the execution of the callback function determines
//(2) Detailed expression :
// If an exception is thrown , new Promise Turn into rejected,reason Exception thrown for
// If yes or no is returned Promise Any value of , new Promise Turn into resolved,value For the return value
// If you return another new Promise, this Promise The result will become new Promise Result
new Promise((resolve, reject) => {
resolve(1)
// reject(2)
}).then(
value => {
console.log('onResolved1()', value)
//return 2
//return Promise.resolve(3)
//return Promise.rekect(4)
},
reason => {
console.log('onrejected1()', reason)
}
).then(
value => {
console.log('onResolved2()', value)
},
reason => {
console.log('onrejected2()', reason)
}
)
版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554519640.html
边栏推荐
- Use case execution of robot framework
- Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
- Quick install mongodb
- ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
- Do you really understand the principle of code scanning login?
- Project framework of robot framework
- On lambda powertools typescript
- Handwritten event publish subscribe framework
- PostgreSQL列存与行存
- Shell script -- shell programming specification and variables
猜你喜欢
Selenium IDE and XPath installation of chrome plug-in
CentOS MySQL multi instance deployment
Bytevcharts visual chart library, I have everything you want
Solution architect's small bag - 5 types of architecture diagrams
STM32__ 03 - beginner timer
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
Node access to Alipay open platform sandbox to achieve payment function
Loggie source code analysis source file module backbone analysis
TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
随机推荐
手写事件发布订阅框架
面试百分百问到的进程,你究竟了解多少
Shell script -- shell programming specification and variables
Kingdee Cloud Star API calling practice
Calculate pie chart percentage
Get the column name list of the table quickly in Oracle
[problem solving] [show2012] random tree
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
vscode如何比较两个文件的异同
Interface document yaml
Sub database and sub table & shardingsphere
Detailed explanation of the penetration of network security in the shooting range
ByteVCharts可视化图表库,你想要的我都有
DDT + Excel for interface test
How vscode compares the similarities and differences between two files
Use case labeling mechanism of robot framework
The new MySQL table has a self increasing ID of 20 bits. The reason is
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Zhongang Mining: Fluorite Flotation Process
[markdown notes]