当前位置:网站首页>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
}
边栏推荐
- JVM performance tuning and practical basic theory - Part 1
- torch建立的网络模型使用torchviz显示
- 个人电脑好用必备软件(使用过)
- 被破解毁掉的国产游戏之光
- Deep anatomy of C language -- C language keywords
- visdom可视化实现与检查介绍
- marathon-envs项目环境配置(强化学习模仿参考动作)
- 根据csv文件某一列字符串中某个数字排序
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- UnsupportedOperationException异常
猜你喜欢
marathon-envs项目环境配置(强化学习模仿参考动作)
【刷题】牛客网面试必刷TOP101
软件卸载时遇到trying to use is on a network resource that is unavailable
Swagger setting field required is mandatory
2022.02.13 - NC004. Print number of loops
MySQL learning record 10getting started with JDBC
Pointer advanced --- pointer array, array pointer
深度剖析C语言指针
PC easy to use essential software (used)
Screenshot in win10 system, win+prtsc save location
随机推荐
Leetcode question brushing (5.28) hash table
704 二分查找
Mobile phones and computers on the same LAN access each other, IIS settings
Colorlog结合logging打印有颜色的日志
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
【刷题】牛客网面试必刷TOP101
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
移位运算符
The harm of game unpacking and the importance of resource encryption
UnsupportedOperationException异常
swagger设置字段required必填
Screenshot in win10 system, win+prtsc save location
Problems in loading and saving pytorch trained models
如何进行接口测试测?有哪些注意事项?保姆级解读
JVM performance tuning and practical basic theory - Part 1
2022.02.13 - 238. Maximum number of "balloons"
LDAP應用篇(4)Jenkins接入
C语言深度解剖——C语言关键字
Is it safe to open an account in Zheshang futures?
MySQL learning record 10getting started with JDBC