当前位置:网站首页>Simple use of promise in uniapp
Simple use of promise in uniapp
2022-07-06 08:42:00 【Rookie post station 2020】
Promise Take two functions as arguments , from Javascript The engine provides , Don't deploy yourself .
resolve Success function | reject Failure function
resolve(): Make current Promise The state of the object is changed to fulfilled
reject(): Make current Promise The object state is changed to rejected
Promise The change of state is one-off , That is to say resolve Function will not be executed reject Function .
var n = 0
// Instantiation promise
let promise = new Promise(function(resolve, reject) {
// Time consuming tasks , Use... In this example setTimeout(...) To simulate asynchronous code
setTimeout(function(){
if(n%2 === 0){
resolve(n)
n = n+1
}else{
reject(n%2)
n = n+1
}
}, 1000)
})
// Method of calling instance
promise.then(result =>{
// When Promise The state of is fulfilled When the
console.log(' success '+ result)
}).catch(error =>{
//1. When Promise Change the status of rejcted , Be performed
//2. When Promise When there is an error in the actuator , Be performed
console.log(' Failure '+ error)
}).finally(()=>{
console.log(" success - Failure - All implemented ")
})
The other is to call functions
click1() {
this.PR1().then(result => {
console.log('resolved Successful callback ')
console.log(' Successfully callback accepted value :', result)
}).catch(error => {
console.log('catch Failed execution callback throw failure reason :', error)
})
},
PR1() {
let p = new Promise(function(resolve, reject) {
setTimeout(function() {
var num = Math.ceil(Math.random() * 20) // Generate 1-20 The random number
console.log(' Values generated by random numbers :', num)
if (num <= 10) {
resolve(num)
} else {
reject(' Number is greater than 10 About to execute the failed callback ')
}
}, 1000)
})
return p
}
边栏推荐
- Light of domestic games destroyed by cracking
- [NVIDIA development board] FAQ (updated from time to time)
- Screenshot in win10 system, win+prtsc save location
- MySQL learning record 07 index (simple understanding)
- 被破解毁掉的国产游戏之光
- 如何进行接口测试测?有哪些注意事项?保姆级解读
- Swagger setting field required is mandatory
- Excellent software testers have these abilities
- swagger设置字段required必填
- Sort according to a number in a string in a column of CSV file
猜你喜欢
Beijing invitation media
MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
2022.02.13 - NC004. Print number of loops
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
2022.02.13 - 238. Maximum number of "balloons"
Bottom up - physical layer
2022.02.13 - NC003. Design LRU cache structure
Chrome浏览器的crash问题
2022.02.13 - NC001. Reverse linked list
Deep analysis of C language pointer
随机推荐
torch建立的网络模型使用torchviz显示
Sublime text using ctrl+b to run another program without closing other runs
深度剖析C语言指针
Generator parameters incoming parameters
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
PLT in Matplotlib tight_ layout()
Sort according to a number in a string in a column of CSV file
Bitwise logical operator
View computer devices in LAN
Image, CV2 read the conversion and size resize change of numpy array of pictures
JS inheritance method
C語言雙指針——經典題型
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
[MySQL] database stored procedure and storage function clearance tutorial (full version)
Research and investment forecast report of citronellol industry in China (2022 Edition)
2022.02.13 - 238. Maximum number of "balloons"
Function coritization
[MySQL] log
ESP8266-RTOS物联网开发