当前位置:网站首页>Promise的理解,以及它的实例方法
Promise的理解,以及它的实例方法
2022-07-27 05:03:00 【weixin_46051260】
是一种一种编程的解决方案
三种状态:pending(进行中),fulfilled(已成功)、rejected(已失败)
特点:状态不受外界影响,只有异步操作的结果,决定当前是哪一种状态
一旦状态改变就不会再改变(pending–>fufilled,pending–>rejected)
用法:
promise是一个构造函数,用来生成promise实例
promise构造函数接收一个函数作为参数,这个函数有两个参数
resolve:将promise对象的状态从未完成变为成功
reject:将promise对象的状态从未完成变为失败
const p=new Promise(function(resolve,reject){
resolve()//返回异步操作的结果,作为参数传递出去
reject()//返回异步操作的结果,作为参数传递出去
})
promise的实例对象
then:当实例状态发生改变时,返回的是一个新的promise实例,也就是promise可以链式书写的原因
catch:用于指定发生错误的回调函数,一把来说通过catch替代then中第二个参数
finally:用来指定不管promise对象状态最后如何,都会执行的操作
const p=new Promise(function(resolve,reject){
setTimeout(() => {
const time=new Date().getTime()
if(time%2==0){
resolve('成功的数据,time'+time)
}else{
reject('失败的数据,time'+time)
}
},1000);
})
//then
p.then((value)=>{
console.log(value)
},(reason)=>{
console.log(reason)
})
//catch
p.then((value)=>{
console.log(value);
}).catch((reason)=> {
console.log(reason);
})
//finally
p.finally(()=>{
console.log('结果');//失败会报错,成功直接返回
})
什么是回调地狱,以及如何用promise解决回调地狱,then
function getData(){
return new Promise((resolve,reject)=>{
setTimeout(() => {
console.log(1);
resolve(2)
}, 1000);
})
}
getData().then((value)=>{
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(value);
resolve(3)
}, 2000);
})
}).then((value)=>{
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(value);
}, 3000);
})
})
promise.all
用于将多个promise实例,包装成一个promise实例
promise.all()参数可以不是数组,但是必须是iterator接口
pAll的状态,由p1、p2、p3来决定,只有当这三个都为成功的时候,pAll才会为成功
但是只要有一个失败,那么就是失败,这个时候第一个失败的实例的返回值,会传递给pAll的回调函数
如果作为参数的实例,自己定义了catch方法,那么它一旦rejected,不会触碰pAll的catch方法
let p1 = new Promise((resolve, reject) => {
resolve('成功01');
})
let p2 = new Promise((resolve, reject) => {
reject('失败02的数据');
}).catch((reason)=>{
console.log('失败');
})
let p3 = new Promise((resolve, reject) => {
resolve('成功02');
})
let pAll=Promise.all([p1,p2,p3])
console.log(pAll);
pAll.then((value)=>{
console.log(value);
}).catch((reason)=>{
console.log(reason);
})
多个请求结果合并在一起
function getBannerList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('轮播图的数据')
}, 1000);
})
}
function getMusicList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('音乐的数据')
}, 2000);
})
}
function getCateList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('歌单分类的数据')
}, 3000);
})
}
function initLoad(){
let All=Promise.all([getBannerList(),getMusicList(),getCateList()])
// console.log(All);
All.then((value)=>{
console.log(value);
})
}
initLoad()
边栏推荐
猜你喜欢

Day4 --- Flask 蓝图与Rest-ful

Graph cuts learning

Utility gadget: kotlin code snippet

Pytorch installation new pit

Day3 ---Flask 状态保持,异常处理与请求钩子

初始C语言——关键字static的作用

李宏毅机器学习组队学习打卡活动day05---网络设计的技巧

Pinball games

Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)

Simplify the mybits framework of JDBC
随机推荐
c语言字符串函数上:strlen、strcpy、strcat
初识C语言——为什么每个C程序都有一个main函数
Simplify the mybits framework of JDBC
李宏毅机器学习组队学习打卡活动day06---卷积神经网络
程序环境和预处理(下):#define、#undef、命令行编译、条件编译、文件包含(超全整理,建议收藏!!!
蓝图-类视图方法
Day3 ---Flask 状态保持,异常处理与请求钩子
如何快速上手强化学习?
数据库迁移报错解决
C WPF uses listbox to implement ruler control
程序环境和预处理(上):一个程序是怎么成功运行的?
js基础练习题
订单系统功能实现
初识C语言——常见的数据类型
Cenos7更新MariaDB
Selenium element operation
Student management system
Flask的传参以及返回的响应
分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)
C语言做一个小迷宫