当前位置:网站首页>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
边栏推荐
- Detailed explanation of Milvus 2.0 quality assurance system
- PHP efficiently reads large files and processes data
- Regular filtering of Intranet addresses and segments
- BUG_ me
- Lock锁
- Use case labeling mechanism of robot framework
- How to implement distributed locks with redis?
- Encapsulating the logging module
- Mock test
- JSON deserialize anonymous array / object
猜你喜欢

Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin

Change the password after installing MySQL in Linux

Milvus 2.0 détails du système d'assurance de la qualité
![[registration] tf54: engineer growth map and excellent R & D organization building](/img/12/7aece45fbc9643c97cdda94b405118.jpg)
[registration] tf54: engineer growth map and excellent R & D organization building

Detailed explanation of Niuke - Gloves
![Customize my_ Strcpy and library strcpy [analog implementation of string related functions]](/img/71/a6a0c7b0e652d2b807f259f7cbf139.png)
Customize my_ Strcpy and library strcpy [analog implementation of string related functions]

Path environment variable

STM32__ 03 - beginner timer

Milvus 2.0 质量保障系统详解

Bottom processing of stack memory in browser
随机推荐
JSON deserialize anonymous array / object
Your brain expands and shrinks over time — these charts show how
聊一聊浏览器缓存控制
Calculate pie chart percentage
[markdown notes]
Node access to Alipay open platform sandbox to achieve payment function
vscode如何比较两个文件的异同
◰GL-阴影贴图核心步骤
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Summary of common websites
Regular filtering of Intranet addresses and segments
About stream flow, write it down briefly------
Bytevcharts visual chart library, I have everything you want
MySQL restores data through binlog file
websocket
Calculation formula related to tolerance analysis
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
1-5 nodejs commonjs specification
How to choose the wireless gooseneck anchor microphone and handheld microphone scheme