当前位置:网站首页>Promise 在uniapp的简单使用
Promise 在uniapp的简单使用
2022-07-06 08:41:00 【菜鸟驿站2020】
Promise接受两个函数作为参数,由Javascript引擎提供,不用自己部署。
resolve 成功函数 | reject 失败函数
resolve(): 使当前Promise对象的状态改成fulfilled
reject(): 使当前Promise对象状态改成rejected
Promise状态的改变是一次性的,即执行了resolve函数后就不会执行reject函数了。
var n = 0
//实例化 promise
let promise = new Promise(function(resolve, reject) {
//需要耗时的任务,在本例中使用setTimeout(...)来模拟异步代码
setTimeout(function(){
if(n%2 === 0){
resolve(n)
n = n+1
}else{
reject(n%2)
n = n+1
}
}, 1000)
})
//调用实例的方法
promise.then(result =>{
//当Promise的状态是fulfilled时执行
console.log('成功'+ result)
}).catch(error =>{
//1. 当Promise的状态改为rejcted ,被执行
//2. 当Promise执行体出现错误时,被执行
console.log('失败'+ error)
}).finally(()=>{
console.log("成功-失败-都执行")
})
另一种是调用函数方式
click1() {
this.PR1().then(result => {
console.log('resolved成功回调')
console.log('成功回调接受的值:', result)
}).catch(error => {
console.log('catch失败执行回调抛出失败原因:', error)
})
},
PR1() {
let p = new Promise(function(resolve, reject) {
setTimeout(function() {
var num = Math.ceil(Math.random() * 20) //生成1-20的随机数
console.log('随机数生成的值:', num)
if (num <= 10) {
resolve(num)
} else {
reject('数字大于10了即将执行失败回调')
}
}, 1000)
})
return p
}
边栏推荐
- Image,cv2读取图片的numpy数组的转换和尺寸resize变化
- Trying to use is on a network resource that is unavailable
- 延迟初始化和密封类
- The mysqlbinlog command uses
- PLT in Matplotlib tight_ layout()
- View computer devices in LAN
- 生成器参数传入参数
- [MySQL] database stored procedure and storage function clearance tutorial (full version)
- Cisp-pte practice explanation
- Bitwise logical operator
猜你喜欢
企微服务商平台收费接口对接教程
2022.02.13 - 238. Maximum number of "balloons"
Let the bullets fly for a while
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
【MySQL】鎖
[brush questions] top101 must be brushed in the interview of niuke.com
win10系统中的截图,win+prtSc保存位置
Excellent software testers have these abilities
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
Sort according to a number in a string in a column of CSV file
随机推荐
Precise query of tree tree
可变长参数
Charging interface docking tutorial of enterprise and micro service provider platform
Trying to use is on a network resource that is unavailable
egg. JS directory structure
sublime text的编写程序时的Tab和空格缩进问题
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
[MySQL] lock
根据csv文件某一列字符串中某个数字排序
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Swagger setting field required is mandatory
Image, CV2 read the conversion and size resize change of numpy array of pictures
Summary of MySQL index failure scenarios
角色动画(Character Animation)的现状与趋势
电脑F1-F12用途
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
704 二分查找
【MySQL】鎖