当前位置:网站首页>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
边栏推荐
- 24. Exchange the nodes in the linked list (linked list)
- 景联文科技—专业数据标注公司和智能数据标注平台
- SQL Server cursor circular table data
- 得到知识服务app原型设计比较与实践
- Full stack cross compilation x86 completion process experience sharing
- 《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
- Kaggle - real battle of house price prediction
- SSH uses private key to connect to server without key
- Charles 功能介绍和使用教程
- SSH利用私钥无密钥连接服务器踩坑实录
猜你喜欢

Yarn core parameter configuration

《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐

SQL Server 递归查询上下级

Visual common drawing (I) stacking diagram

VIM + ctags + cscope development environment construction guide

Download and installation steps of xshell + xftp

GO接口使用

Chapter 120 SQL function round

Comparison and practice of prototype design of knowledge service app

关于JUC三大常用辅助类
随机推荐
使用zerotier让异地设备组局域网
JVM - common parameters
209. Subarray with the smallest length (array)
Learning notes 7-depth neural network optimization
SSH uses private key to connect to server without key
微信小程序中app.js文件、组件、api
Visualization Road (11) detailed explanation of Matplotlib color
Xdotool key Wizard
C language - custom type
Intuitive understanding entropy
Is the pointer symbol of C language close to variable type or variable name?
Source insight 4.0 FAQs
Xshell+Xftp 下载安装步骤
Visual common drawing (IV) histogram
【leetcode】107.二叉树的层序遍历II
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
How to Ping Baidu development board
SQL Server 递归查询上下级
比深度学习更值得信赖的模型ART
部署jar包