当前位置:网站首页>es6-promise对象详解
es6-promise对象详解
2022-08-10 12:07:00 【forwardMyLife】
1.promise简介
promise是es6中新出的一个对象,主要用来获取异步操作成功或者失败的结果。promise是js异步编程中的核心对象,es8中await和async也是基于promise实现的
promise对象有3种状态,
| 状态 | 描述 |
|---|---|
| pending | 等待中,还未执行完 |
| resolved | 执行成功 |
| rejected | 执行失败 |
状态变更:只有2种
pending => resolved
pending => rejected
2.promise的优点
避免回调地狱,promise对象执行完后,可以在then方法,或catch方法中,获取结果,then方法中也会返回一个promise对象(即便本身返回的是普通对象最终也被包装成promise对象)
3.promise对象的创建
let promise = new Promise((resovle,reject)=>{
console.info("create promise object");
setTimeout(() => {
resovle("ok");
});
}).then(value=>{
console.info("successed to promise,returned value:"+value);
},reason=>{
console.info("failed to execute promise,reason"+reason);
}).catch(reason=>{
console.error(`failed to reason:${
reason}`);
});;
promise.then(value=>{
console.info("then:"+value);
});
console.info("end js ......");
promise对象的入参单个箭头函数,箭头函数2个入参为2个函数,resolve,reject,用于成功或失败的状态变更。
resolve的结果为then方法的第一个箭头函数的value参数,
如果then设置了第二个参数,则promise在被reject或抛出异常的情况下会执行reason的箭头函数。
then也会返回一个新的promise对象。
then方法中会等到同步代码块执行完,才会执行。而promise实例化的代码是同步执行的。
整个执行是异步的。
如上述代码执行结果如下:
4.promise常用静态方法
1.promise的对象方法(p1,p2,p3为promise的实例对象)
Promise.all()并发处理多个异步任务,所有任务都执行完成才能得到结果
Promise.all( [p1,p2,p3] ) .then ( (result) => {
console.info (result);
})
2.Promise.race()并发处理多个异步任务,只要有一个任务完成就会返回结果
Promise.all()并发处理多个异步任务,所有任务都执行完成才能得到结果
Promise.race ( [p1,p2,p3] ).then ( (result)=>{
console.log (result)
})
边栏推荐
- 人脸考勤是选择人脸比对1:1还是人脸搜索1:N?
- It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
- 2022年8月中国数据库排行榜:openGauss重夺榜眼,PolarDB反超人大金仓
- 厚积薄发!安全狗再次获得科技成果转化认证!
- Dining (网络流)
- 阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
- tommy's spell
- 如何让别人看不懂你的 JS 代码?把你当大佬!
- LeetCode 237. Delete a node in a linked list
- 第5章 虚拟存储器
猜你喜欢

面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官

培训机构学习费用是多少呢?

如何让别人看不懂你的 JS 代码?把你当大佬!

LT8911EXB MIPI CSI/DSI to EDP signal conversion

一文详解 implementation api embed

Is there a problem with the CURRENT_TIMESTAMP(6) function?

Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?

【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)

dedecms支持Word内容一键导入

漏洞管理计划的未来趋势
随机推荐
mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
Guo Jingjing's personal chess teaching, the good guy is a robot
A detailed explanation of implementation api embed
Drive IT Modernization with Low Code
你是怎么知道数据库 Htap 能力强弱的?怎么能看出来
解决 idea 单元测试不能使用Scanner
一文详解 implementation api embed
漏洞管理计划的未来趋势
“68道 Redis+168道 MySQL”精品面试题(带解析)
【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
部署项目半途而废后续
IP地址分类以及网络地址的计算(子网划分、超网划分)[通俗易懂]
LeetCode 86. Delimited Linked List
iTextSharp 使用详解
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
CodeForces - 628D (数位dp)
MySQL相关问题整理
日记16
多线程下自旋锁设计基本思想
An enhanced dynamic packet buffer management. The core part of the paper