当前位置:网站首页>Detailed explanation of es6-promise object
Detailed explanation of es6-promise object
2022-08-10 12:56:00 【forwardMyLife】
1.promise简介
promise是es6A new object in ,It is mainly used to obtain the result of the success or failure of an asynchronous operation.promise是jsCore objects in asynchronous programming,es8中await和async也是基于promise实现的
promise对象有3种状态,
| 状态 | 描述 |
|---|---|
| pending | 等待中,还未执行完 |
| resolved | 执行成功 |
| rejected | 执行失败 |
状态变更:只有2种
pending => resolved
pending => rejected
2.promise的优点
避免回调地狱,promise对象执行完后,可以在then方法,或catch方法中,获取结果,thenmethod will also return onepromise对象(Even if it returns a normal object itself, it is eventually wrappedpromise对象)
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 ......");
promiseThe object's argument is a single arrow function,箭头函数2The input parameter is2个函数,resolve,reject,Status change for success or failure.
resolve的结果为thenThe first arrow function of the methodvalue参数,
如果then设置了第二个参数,则promise在被rejector if an exception is thrownreason的箭头函数.
then也会返回一个新的promise对象.
thenThe method will wait until the synchronized block of code is executed,才会执行.而promiseThe instantiated code is executed synchronously.
The entire execution is asynchronous.
The result of executing the above code is as follows:
4.promise常用静态方法
1.promise的对象方法(p1,p2,p3为promise的实例对象)
Promise.all()并发处理多个异步任务,所有任务都执行完成才能得到结果
Promise.all( [p1,p2,p3] ) .then ( (result) => {
console.info (result);
})
2.Promise.race()并发处理多个异步任务,The result is returned as long as one task completes
Promise.all()并发处理多个异步任务,所有任务都执行完成才能得到结果
Promise.race ( [p1,p2,p3] ).then ( (result)=>{
console.log (result)
})
边栏推荐
- 【mysql索引实现原理】
- 【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
- 国外媒体宣发怎样做才可以把握重点
- LT8911EXB MIPI CSI/DSI to EDP signal conversion
- wirshark 常用操作及 tcp 三次握手过程实例分析
- Guo Jingjing's personal chess teaching, the good guy is a robot
- 11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
- es6-promise对象详解
- Real-time data warehouse practice of Baidu user product flow and batch integration
- 基于PLECS的离网(孤岛)并联逆变器的Droop Control下垂控制仿真
猜你喜欢

【百度统计】用户行为分析

Proprietary cloud ABC Stack, the real strength!

关于flask中static_folder 和 static_url_path参数理解
What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch

多线程下自旋锁设计基本思想

MySQL面试题——MySQL常见查询

CURRENT_TIMESTAMP(6) 函数是否存在问题?

iTextSharp操作PDF

第5章 虚拟存储器

线代 | 秒杀方法与技巧
随机推荐
【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
一文详解 implementation api embed
Comparison version number of middle questions in LeetCode
StarRocks on AWS 回顾 | Data Everywhere 系列活动深圳站圆满结束
金山云要飘到哪里?
爱可可AI前沿推介(8.10)
MYSQL误删数据恢复
关于flask中static_folder 和 static_url_path参数理解
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
H264 码率控制
LeetCode简单题之合并相似的物品
You have a Doubaqiong thesaurus, please check it
Codeforces Round #276 (Div. 1) D. Kindergarten
Highways「建议收藏」
LeetCode medium topic search of two-dimensional matrix
mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
娄底污水处理厂实验室建设管理
什么是云流化?
九宫格抽奖动效