当前位置:网站首页>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
}

边栏推荐
- R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
- Light of domestic games destroyed by cracking
- Generator parameters incoming parameters
- 深度剖析C语言指针
- [brush questions] top101 must be brushed in the interview of niuke.com
- Golang force buckle leetcode 1020 Number of enclaves
- 【Nvidia开发板】常见问题集 (不定时更新)
- Trying to use is on a network resource that is unavailable
- 游戏解包的危害及资源加密的重要性
- 软件卸载时遇到trying to use is on a network resource that is unavailable
猜你喜欢

IOT -- interpreting the four tier architecture of the Internet of things

Light of domestic games destroyed by cracking

企微服务商平台收费接口对接教程

【MySQL】日志

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers

【刷题】牛客网面试必刷TOP101

ROS compilation calls the third-party dynamic library (xxx.so)
![[MySQL] lock](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
[MySQL] lock

PC easy to use essential software (used)

优秀的软件测试人员,都具备这些能力
随机推荐
On the inverse order problem of 01 knapsack problem in one-dimensional state
延迟初始化和密封类
C语言双指针——经典题型
Light of domestic games destroyed by cracking
Chrome浏览器的crash问题
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
Golang force buckle leetcode 1020 Number of enclaves
PC easy to use essential software (used)
Deep analysis of C language data storage in memory
JVM 快速入门
marathon-envs项目环境配置(强化学习模仿参考动作)
[MySQL] lock
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
JS native implementation shuttle box
[MySQL] database stored procedure and storage function clearance tutorial (full version)
Synchronized solves problems caused by sharing
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
China polyether amine Market Forecast and investment strategy report (2022 Edition)
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Charging interface docking tutorial of enterprise and micro service provider platform