当前位置:网站首页>Promise详解
Promise详解
2022-04-23 10:53:00 【Mu_Mu是一只小白】
1.基本概念:
Promise是JS异步编程中的重要概念, 异步抽象处理对象,是目前比较流行Javascript异步编程解决方案之一
2.Promise的三种状态:
pending:对象初始化状态
fulfilled:当调用resolve(成功),会由pending => fulfilled
rejected:当调用reject(失败),会由pending => rejected
3.创建Promise
3.1 new Promise(resolve, reject);
const promise = new Promise((resolve, reject) => {
resolve('fulfilled'); // 状态由 pending => fulfilled
//reject('rejected'); // 状态由 pending => rejected
});
//then 在Promise 状态发生改变时触发
promise.then(result => { // onFulfilled resolve被调用会进入
console.log(result); },
reason => { // onRejected ,reject被调用会进入
})
3.2 c的静态方法创建
3.2.1 Promise.resolve() 返回一个fulfilled状态的promise对象
3.2.2 Promise.reject() 返回一个rejected状态的promise对象
Promise.reject() 不管传给它什么值,它会直接把这个值传递给错误回调函数。
Promise.resolve() 如何执行取决于传递的参数。
1)传普通类型(数值等)
let p1 = new Promise(resolve => {
resolve('成功');
})
//和上面是等价的
let p2 = Promise.resolve('成功'); //会直接决议为成功并把这个值传递过去
2)Promise类型:
let param = new Promise( resolve =>{
resolve('----resolve----')
})
//直接返回传递进去的promise
let p = Promise.resolve(param); //直接将param实例返回给了p
p.then(data => {console.log(data)}) //后打印结果为----resolve----
console.log( p === param) //打印true,虽然在p.then的下方但是由于Promise异步执行会先打印打印true
3) 传递一个thenable对象
let obj = { //obj是一个thenable对象
then(callback){
console.log('-----then---------');
callback('-------callback-----')
},
other(){
console.log('--------other---------') //不打印
}
}
Promise.resolve(obj)
执行结果:
Promise.resolve(obj).then(data => {
console.log(data)
})
执行结果:
结论:传进thenable对象时,会立即执行then方法,如果then方法中有回调,会在Promise.resolve(obj).then()时调用(个人理解此时then里面的匿名函数 data => {
console.log(data)
} 替代了callback)。
版权声明
本文为[Mu_Mu是一只小白]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lin1214000999/article/details/124273508
边栏推荐
- Linked list intersection (linked list)
- Visual solutions to common problems (VIII) mathematical formulas
- Gets the current time in character format
- 707. Design linked list (linked list)
- 【leetcode】107. Sequence traversal of binary tree II
- SQL Server 游标循环表数据
- JDBC – PreparedStatement – 如何设置 Null 值?
- C#和数据库连接中类的问题
- App. In wechat applet JS files, components, APIs
- How to Ping Baidu development board
猜你喜欢
Six practices of Windows operating system security attack and defense
mysql同一个表中相同数据怎么合并
Swagger2 接口如何导入Postman
【leetcode】107. Sequence traversal of binary tree II
SSH利用私钥无密钥连接服务器踩坑实录
高价买来的课程,公开了!phper资料分享
Solution architect's small bag - 5 types of architecture diagrams
Visualized common drawing (II) line chart
Comparison and practice of prototype design of knowledge service app
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
随机推荐
Restful、SOAP、RPC、SOA、微服务之间的区别
Code implementation of general bubbling, selection, insertion, hill and quick sorting
707. Design linked list (linked list)
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
Data analysis learning (I) data analysis and numpy Foundation
MapReduce compression
Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
MySql常用语句
Charles 功能介绍和使用教程
Leetcode22: bracket generation
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
学习网站资料
CentOS/Linux安装MySQL
使用zerotier让异地设备组局域网
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
STM32接电机驱动,杜邦线供电,然后反烧问题
The courses bought at a high price are open! PHPer data sharing
How does the swagger2 interface import postman
202. Happy number
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition