当前位置:网站首页>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
}
边栏推荐
- Revit 二次开发 HOF 方式调用transaction
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- 【MySQL】鎖
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- 如何有效地进行自动化测试?
- marathon-envs项目环境配置(强化学习模仿参考动作)
- 根据csv文件某一列字符串中某个数字排序
- Shift Operators
- Screenshot in win10 system, win+prtsc save location
- [MySQL] database stored procedure and storage function clearance tutorial (full version)
猜你喜欢
Image, CV2 read the conversion and size resize change of numpy array of pictures
Charging interface docking tutorial of enterprise and micro service provider platform
游戏解包的危害及资源加密的重要性
2022.02.13 - 238. Maximum number of "balloons"
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
C语言深度解剖——C语言关键字
MySQL learning record 10getting started with JDBC
Light of domestic games destroyed by cracking
tree树的精准查询
2022.02.13 - NC004. Print number of loops
随机推荐
JVM 快速入门
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Indentation of tabs and spaces when writing programs for sublime text
[brush questions] top101 must be brushed in the interview of niuke.com
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
Charging interface docking tutorial of enterprise and micro service provider platform
从表中名称映射关系修改视频名称
JS inheritance method
win10系统中的截图,win+prtSc保存位置
Double pointeur en langage C - - modèle classique
有效提高软件产品质量,就找第三方软件测评机构
egg. JS project deployment online server
Deep analysis of C language data storage in memory
JVM performance tuning and practical basic theory - Part 1
C语言双指针——经典题型
电脑清理,删除的系统文件
[MySQL] database stored procedure and storage function clearance tutorial (full version)
ROS编译 调用第三方动态库(xxx.so)
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
[MySQL] lock