当前位置:网站首页>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)
})
边栏推荐
- 22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
- iTextSharp 使用详解
- Real-time data warehouse practice of Baidu user product flow and batch integration
- Jenkins修改默认主目录
- Solve the idea that unit tests cannot use Scanner
- 「企业架构」应用架构概述
- 【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
- Drive IT Modernization with Low Code
- 百度用户产品流批一体的实时数仓实践
- 48 the mysql database
猜你喜欢
百度用户产品流批一体的实时数仓实践
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
iTextSharp操作PDF
MYSQL误删数据恢复
Solve the idea that unit tests cannot use Scanner
Chapter9 : De Novo Molecular Design with Chemical Language Models
解决 idea 单元测试不能使用Scanner
如何让别人看不懂你的 JS 代码?把你当大佬!
LT8911EXB MIPI CSI/DSI to EDP signal conversion
随机推荐
【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
娄底干细胞制备实验室建设须知要求
部署项目半途而废后续
线代 | 秒杀方法与技巧
CV复习:空洞卷积
Polygon zkEVM工具——PIL和CIRCOM
48 the mysql database
【集合】HashSet和ArrayList的查找Contains()时间复杂度
Diary 16
An enhanced dynamic packet buffer management. The core part of the paper
Pod生命周期
Merge similar items in LeetCode simple questions
第5章 虚拟存储器
娄底疾控中心实验室设计理念说明
A detailed explanation of implementation api embed
实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
LeetCode简单题之合并相似的物品
娄底农产品检验实验室建设指南盘点
用低代码驱动IT现代化
培训机构学习费用是多少呢?